gpt4 book ai didi

java - 如何在不使用扫描仪的情况下将字符/算术运算符作为用户的输入

转载 作者:太空宇宙 更新时间:2023-11-04 10:47:25 25 4
gpt4 key购买 nike

感谢您查看我的问题。我想问,我可以在不使用扫描仪的情况下将字符或算术运算符作为用户的输入吗?

这是我编写一个程序的代码,使用算术运算符对两个数字执行代数运算。 (代数运算为+、-、*、/、%)

import java.util.Scanner;
class q4
{
public static void main(String args[])
{
Scanner s = new Scanner(System.in );
int a, b;
char operator;
System.out.print("Enter A : ");
a=s.nextInt();
System.out.print("Enter B : ");
b=s.nextInt();
System.out.print("Enter operator (+, -, *, /)");
operator = s.next().charAt(0);
double addition = a+b;
double subtraction = a-b;
double multiplication = a*b;
double division = a/b;

switch(operator)
{
case '+' :
{
System.out.print("Total after Addition is : "+addition);
break;
}
case '-' :
{
System.out.print("Total after Subtraction is : " +subtraction);
break;
}
case '*' :
{
System.out.print("Total after Multiplication is : "+multiplication);
break;
}
case '/' :
{
System.out.print("Total after Division is : "+division);
break;
}
default :
{
System.out.print("Please select proper operator");
return;
}
}
}
}

感谢您在百忙之中回复我:)

最佳答案

我不太确定在这种情况下为什么不使用 Scanner...但是,这些是在不使用它的情况下读取用户输入的其他一些方法:

  1. 使用JOptionPane ( read more about it here )

用途:

 a = Integer.parseInt(JOptionPane.showInputDialog("Enter A :"))

而不是:

System.out.print("Enter A : "); 
a=s.nextInt();

将其用于所有不同的输入。

  • 使用 System.in 创建读取器变量,并使用 BufferedReader 定义变量 a 来获取输入:

    InputStreamReader isr = new InputStreamReader(System.in); 
    BufferedReader br = new BufferedReader (isr);
    try {
    a = Integer.parseInt(br.readLine());
    } catch (IOException e) {
    e.printStackTrace();
    }
  • 顺便说一句,在知道用户想要哪个操作之前,您无需执行所有操作。更改此:

    double addition  = a+b;
    double subtraction = a-b;
    double multiplication = a*b;
    double division = a/b;

    switch(operator)
    {
    case '+' :
    {
    System.out.print("Total after Addition is : "+addition);
    break;
    }
    case '-' :
    {
    System.out.print("Total after Subtraction is : " +subtraction);
    break;
    }
    case '*' :
    {
    System.out.print("Total after Multiplication is : "+multiplication);
    break;
    }
    case '/' :
    {
    System.out.print("Total after Division is : "+division);
    break;
    }
    default :
    {
    System.out.print("Please select proper operator");
    return;
    }
    }

    仅此:

    switch(operator)
    {
    case '+' :
    {
    double addition = a+b;
    System.out.print("Total after Addition is : "+addition);
    break;
    }
    case '-' :
    {
    double subtraction = a-b;
    System.out.print("Total after Subtraction is : " +subtraction);
    break;
    }
    case '*' :
    {
    double multiplication = a*b;
    System.out.print("Total after Multiplication is : "+multiplication);
    break;
    }
    case '/' :
    {
    double division = a/b;
    System.out.print("Total after Division is : "+division);
    break;
    }
    default :
    {
    System.out.print("Please select proper operator");
    return;
    }
    }

    关于java - 如何在不使用扫描仪的情况下将字符/算术运算符作为用户的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48239579/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com