-3

I want write a program that shows menu and asks the user to select from two choices a) Find odd numbers : The program asks the user to enter integer number x, then prints the odd integers from x to 1 b) Find even numbers: The program asks the user to enter integer number x, then prints the even integers from x to 2. - Your code should recognize the invalid characters and display "invalid choice!!" message. - The program should display the message “Do you want to continue ? y or n “ after each calculation. The user will enter ‘y’ to continue, otherwise the user will enter 'n'. I wrote my code like this

import java.util.*;

public class choices{
    //main method
    public static void main(String args[]){
        Scanner input=new Scanner(System.in);
        String op;
        int num;

        String op2;
        String odd="";
        String even="";
        int count=0;

        do{
            System.out.println("a) Find odd numbers");
            System.out.println("b) Find even numbers");
            System.out.print("choose an option [ a, b]: ");
            op=input.next();
            if (op.equals('a'))
                System.out.print("Enter Integer number : ");
            num=input.nextInt();

            for(int i=num ; i >=num-i ;i--){
                if(num%2!=0)
                    odd=odd+""+i;
                System.out.print("odd numbers are : "+odd );
            }

            if (op.equals('b')){
                for(int i=num ; i >=num-i ;i--){
                    if(num%2==0)
                        even=even+""+i;      
                    System.out.print("even numbers are : "+even );
                }
            }
            else
                System.out.println("invalid choice");

            System.out.print("Do you want to continue (y or n)?");
            op2=input.next();  


            if(op2.equals('n')) 
                System.out.println("Bye");
        }

        while(!op2.equals('n'));
        count++;



    }
}

but the problem is : it stop after > choose an option [ a, b]: b

a) Find odd numbers
b) Find even numbers
choose an option [ a, b]: b
d
Exception in thread "main" java.util.InputMismatchException
       at java.util.Scanner.throwFor(Scanner.java:909)
       at java.util.Scanner.next(Scanner.java:1530)
       at java.util.Scanner.nextInt(Scanner.java:2160)
       at java.util.Scanner.nextInt(Scanner.java:2119)
       at choices.main(choices.java:30)
4

3 回答 3

2
System.out.print("choose an option [ a, b]: ");
op=input.next();
if (op.equals('a'))
System.out.print("Enter Integer number : ");
num=input.nextInt();

请注意您的 if 范围。很可能是错的。您每次都在调用 nextInt() 。

于 2013-11-05T21:00:14.203 回答
0

在这一行之后op=input.next();(可以是 a 或 b)下一个是 this num=input.nextInt();,你不能选择d,因为它不是 Int。尝试捕获异常:

boolean stopFlag = false;

do {
...
System.out.print("choose an option [ a, b]: ");
op=input.next();
if (op.equals('a')) { // i think `{` must be here
    System.out.print("Enter Integer number : ");
    try {
       num=input.nextInt();
    } catch (InputMismatchException ex) {
       // this wasn't an int 
       System.out.println("You entered not a correct number.");
       System.out.println("Bye");
       stopFlag = true;
    }
    ///
    for(int i=num ; i >=num-i ;i--){
        if(num%2!=0)
            odd=odd+""+i;
        System.out.print("odd numbers are : "+odd );
    }
} // and `}` here
...
// make the same with `b`
...
while(!stopFlag);
于 2013-11-05T21:11:28.063 回答
0

你有几个问题。

首先,您需要将 if 语句的代码包装在大括号中。这就是我假设你想要的:

if (op.equals('a'))
{
    System.out.print("Enter Integer number : ");
    num=input.nextInt();

    for(int i=num ; i >=num-i ;i--)
    {
        if(num%2!=0)
            odd=odd+""+i;
        System.out.print("odd numbers are : "+odd );
    }
}

其次,'a'是一个角色,所以你的 if 条件永远不会通过。你需要做op.equals("a")。问题是它跳过了您的第二个提示,因为 if 语句失败,然后您"d"出于input.nextInt()某种原因喂它,并且您收到类型错误。

于 2013-11-05T21:03:38.067 回答