验证字符用户输入循环

huangapple go评论75阅读模式
英文:

validating char user input loop

问题

我正在编写一个简单的计算器程序,用户输入两个整数和一个表示操作(加法、减法、乘法等)的符号(+ - * 等),程序计算他们所请求的操作。

我使用了一个 do-while 循环,如果输入与给定的符号不匹配,就会不断地要求输入。

我尝试过进行比较,但我不成功,并且对要做什么非常不确定。

代码:

Scanner scan = new Scanner(System.in);

int num1, num2, sum, difference, product;

String a, b, c, userInput;

char choice, plus, minus, times;

System.out.print("第一个整数: --> ");
num1 = scan.nextInt();

System.out.print("第二个整数: --> ");
num2 = scan.nextInt();

a = "+";
b = "-";
c = "*";

plus = a.charAt(0);
minus = b.charAt(0);
times = c.charAt(0);

do {
    System.out.print("选择 + - 或 *:");
    userInput = scan.next();

    choice = userInput.charAt(0);
} while(choice != '+' || choice != '-' || choice != '*');

switch(choice) {
     case '+':
     sum = num1 + num2;
     System.out.println("和为:" + sum);
     break;

     case '-':
     difference = num1 - num2;
     System.out.println("差为:" + difference);
     break;

     case '*':
     product = num1 * num2;
     System.out.println("积为:" + product);
     break;
} 

注意:我只翻译了代码部分,不包括你的问题。如果你有任何关于代码的问题或需要进一步帮助,请随时问我。

英文:

I'm writing a simple calculator program, the user inputs two integers and a symbol (+ - * etc) which represents the operation (add, subtract, multiply, etc) to be applied to the integers and the program calculates what they asked.

I have a do-while loop to continually ask for input if the input does not match the given symbols.

I've tried comparing them but I'm unsuccessful and very uncertain on what to do

CODE:

Scanner scan = new Scanner(System.in);

int num1, num2, sum, difference, product;

String a, b, c, userInput;

char choice, plus, minus, times;

System.out.print(First Integer: --> ");
num1 = scan.nextInt();

System.out.print(Second Integer: --> ");
num2 = scan.nextInt();

a = "+";
b = "-";
c = "*";

plus = a.charAt(0);
minus = b.charAt(0);
times = c.charAt(0);

do {
    System.out.print("Choose + - or *");
    userInput = scan.Next();

    choice = userInput.charAt(0);
} while(choice != '+' || choice != '-' || choice != '*');

switch(choice) {
     case 1:
     sum = num1 + num2;
     System.out.println("Sum is " + sum);
     break;

     case 2:
     difference = num1 - num2;
     System.out.println("Difference is " + difference);
     break;

     case 3:
     product = num1 ' num2;
     System.out.println("Product is " + product);
     break;
} 

答案1

得分: 2

你的代码中有一些错误,我已经尝试纠正它们。请看:

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int num1, num2, sum, difference, product;
        String userInput;
        char choice, plus, minus, times;

        System.out.println("第一个整数: --> ") ;
        num1 = scan.nextInt();

        System.out.println("第二个整数: --> ") ;
        num2 = scan.nextInt();

        plus = '+';
        minus = '-';
        times = '*';

        do {
            System.out.println("选择 + - 或 *") ;
            userInput = scan.next();
            choice = userInput.charAt(0);
        } while(choice != '+' && choice != '-' && choice != '*');
        
        switch(choice) {
            case '+':
                sum = num1 + num2;
                System.out.println("和是 " + sum);
                break;

            case '-':
                difference = num1 - num2;
                System.out.println("差是 " + difference);
                break;

            case '*':
                product = num1 * num2;
                System.out.println("积是 " + product);
                break;
                 
            default :
                System.out.println("错误的选择!");
                break;
        }
    }
}

如果你需要更多帮助或有其他问题,请随时告诉我。

英文:

There were some errors in your code, I have tried to rectify them.
Take a look,

public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int num1, num2, sum, difference, product;
String userInput;
char choice, plus, minus, times;
System.out.println("First Integer: --> ") ;
num1 = scan.nextInt();
System.out.println("Second Integer: --> ") ;
num2 = scan.nextInt();
plus = '+';
minus = '-';
times = '*';
do {
System.out.println("Choose + - or *") ;
userInput = scan.next();
choice = userInput.charAt(0);
}while(choice != '+' && choice != '-' && choice != '*');
switch(choice) {
case '+':
sum = num1 + num2;
System.out.println("Sum is " + sum);
break;
case '-':
difference = num1 - num2;
System.out.println("Difference is " + difference);
break;
case '*':
product = num1 * num2;
System.out.println("Product is " + product);
break;
default :
System.out.println("Wrong Choice!");
break;
}
}
}

huangapple
  • 本文由 发表于 2020年9月7日 09:55:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63770502.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定