【问题标题】:Can't understand this scanner or addition command无法理解此扫描仪或添加命令
【发布时间】:2014-08-08 21:04:46
【问题描述】:

我对 Java 很陌生,并不太完全理解它,我正在处理 Uni 研讨会任务,但在这个特定问题上遇到了麻烦。

“编写一个程序,要求用户输入他们使用了多少分钟,以及他们使用了多少文本。 两个输入都应该是整数(整数)。 然后程序应该计算用户的手机账单,假设短信费用为 7 便士,通话费用为 12 便士。 应该显示电话费、短信费和总账单,这两个数字相加”

Scanner userInput = new Scanner(System.in);                             
System.out.println("How many minutes have you used?");
String one = userInput.nextLine();
System.out.println("How many texts have you used?"); 
String two = userInput.nextLine(); 
int a = 12; 
int b = 7;
System.out.println("The total cost of your minutes is "+one); 
System.out.println("The total cost of you texts is "+two); 
System.out.println("The total cost of your phone bill is "+one + two);

我已经弄清楚了这个问题的基本部分,但不知道为什么我不能添加代码来计算价格,分钟为 12 便士,文本为 7 便士。除此之外,我无法将电话费的总费用正确加在一起。我之前做过,我知道这很容易,但我完全忘记了如何去做。

我知道我需要能够更好地理解扫描仪,但我之前的任务很容易完成,但这真的让我很难过。我是否需要重命名扫描仪,但是当我将整数行的名称更改为“totalCostOfTexts/Minutes 等”时,它要么说它已经定义,要么缺少某种符号。

感谢任何反馈。

我添加代码:

int = userInput = minutes * 12:

因为这是在类似问题的前一部分中使用的,但我得到的所有反馈都是它不是陈述,因此无法处理。我真的在努力解决这个问题。

【问题讨论】:

  • "你话费的总费用是 "+one + two 应该是 "你话费的总费用是 "+(one + two)
  • 请注意,您的变量名称是随机的,与它们的内容无关。名称应描述其中存储的内容。

标签: java java.util.scanner addition


【解决方案1】:

以下代码将为您工作

Scanner userInput = new Scanner(System.in);                             
System.out.println("How many minutes have you used?");
int one = userInput.nextInt();
System.out.println("How many texts have you used?"); 
int two = userInput.nextInt(); 
int a = 12;      //don't use such variable names
int b = 7;       
int minute_bill=12*a;   //see the variable,will make things easier to review
int text_bill=7*b;
int result=minute_bill+text_bill;
System.out.println("The total cost of your minutes is "+minute_bill); 
System.out.println("The total cost of you texts is "+ text_bill); 
System.out.println("The total cost of your phone bill is "+result);

还有

  • 您可以使用Scanner'snextInt()方法获取integer输入 从控制台。
  • 不要使用像a,b这样的变量名。根据你在其中存储的值的属性来定义它们(见上文minute_billtext_bill使代码干净且易于查看)
  • 如果你一定要从控制台获取字符串值,但想稍后将输入的值转换为整数,那么你可以像下面的代码那样做

    String mystring=userInput.nextLine();   //userInput is user Scanner's object
    int num=Integer.parseInt(mystring);
    

【讨论】:

    【解决方案2】:

    我认为这就是你想要做的......

        Scanner userInput = new Scanner(System.in);                             
        System.out.println("How many minutes have you used?");
        int one = Integer.valueOf(userInput.nextLine());
        System.out.println("How many texts have you used?"); 
        int two= Integer.valueOf(userInput.nextLine());
        int a = 12; 
        int b = 7;
        System.out.println("The total cost of your minutes is "+ (one * 12); 
        System.out.println("The total cost of you texts is "+ (two * 7)); 
        System.out.println("The total cost of your phone bill is "+ ((one * 12) + (two * 7));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-09
      • 1970-01-01
      相关资源
      最近更新 更多