0

我被要求为使用 Java 的新工作中的培训练习创建一个简单的输入输出代码。我是一个 Java 新手,所以我遇到了一些困难。这是我目前的代码...

import java.util.*;
  public class Task3
  {
  static Scanner console = new Scanner(System.in);
        public static void main(String[] args)
        {
              {
                    int Number1;
                    int Number2;
                    int Number3;
                    int Sum;
                   Number1= console.nextInt(); 
                   Number2= console.nextInt();
                   Number3= console.nextInt();
                    Volume = Number1 * Number2 + Number3 ;
              }

              System.out.printIn("Answer:" + Sum ) ;

        }
  }

实际上,我希望此代码从用户读取 3 个数字作为输入,然后生成 Number1 x Number2 + Number3 的总和作为输出。我确信有几种方法可以做到这一点,但我上面开始的方式是我们被要求这样做的方式。任何帮助将不胜感激,因为我很想了解更多关于这方面的信息以及我遇到麻烦的地方......提前谢谢,约翰

4

2 回答 2

0

只是为了添加三个数字。程序应该是

 public static void main(String[] args) throws IOException {    
        Scanner s = new Scanner(System.in);
        int num1= s.nextInt();
        int num2= s.nextInt();
        int num3= s.nextInt();
        System.out.println("Sum= "+ (num1+ num2+num3));
        // or play any game with those variables here
    }

您正在添加额外的{}变量而没有任何用处。摆脱那些。

于 2013-10-09T14:17:08.170 回答
0

检查这个:

import java.util.*;

public class MyJavaProgram
{
    static Scanner console = new Scanner(System.in);

    public static void main(String[] args) {    
        int number1 = console.nextInt();
        int number2 = console.nextInt();
        int number3 = console.nextInt();
        System.out.println("Volume = "+ ((number1 * number2) + number3));
    }
}

您的代码的更改:

  • Removed a set of brackets which were creating a local scope where Sum was defined, yet you were trying to use Sum outside of that local scope
  • Variable defined as Sum but then referred to as Volume; I assume this was a copy & paste error.
于 2013-10-09T14:18:19.210 回答