【问题标题】:How to Declare and read values into integer variables?如何声明并将值读入整数变量?
【发布时间】:2014-10-25 19:33:38
【问题描述】:

我真的是 Java 新手,我正在上计算机科学的入门课程。我需要知道如何提示用户输入两个值,声明和定义 2 个变量来存储整数,然后能够读取这些值,最后将这些值打印出来。但我很迷茫,我什至不知道如何开始我花了一整天的时间尝试.. 我真的需要一些帮助/指导。我需要对整数、十进制数和字符串这样做。有人可以帮我吗?

这是我尝试过的

import java.util.Scanner;

class VariableExample
{ 
Scanner scan = new Scanner(System.in);

System.out.println("Please enter an integer value");
int a = scan.nextInt(); 
int b = scan.nextInt();

System.out.println("Please enter an integer value");
double c = scan.nextDouble(); 
double d = scan.nextDouble();

System.out.println("Please enter an integer value");
string e = scan.next();    
string f = scan.next();

System.out.println("Your integer is: " + intValue + ", your real number is: "
                            + decimalValue + " and your string is: " + textValue);
}

我告诉过你...我真的很新

【问题讨论】:

  • 你忘了声明 main 方法,并把代码放在里面。当代码无法编译并且您想知道为什么时,您应该做的第一件事是阅读并弄清楚错误消息的含义。如果你不理解他们,然后谷歌他们。然后发布它们。忽略错误消息是一个巨大的错误。
  • 我想你需要问问你的导师或助教。对他们来说,知道课堂上什么时候没有正确解释课程非常重要。说了这么多,有什么问题吗?你还没有说你真正需要帮助的是什么。
  • 您在一小时前接受了this 的回答。因此,您知道如何进行此操作。不是吗?
  • 感谢您的帮助,感谢大家,我已经解决了问题

标签: java


【解决方案1】:

您忘记声明入口点,即 main() 方法,String S 应该是大写,而在 System.out.println() 中您使用了错误的变量:

class VariableExample { 
   public static void main(String... args) { // Entry point..
    Scanner scan = new Scanner(System.in);

    System.out.println("Please enter an integer value");
    int a = scan.nextInt();
    int b = scan.nextInt();

    System.out.println("Please enter an integer value");
    double c = scan.nextDouble();
    double d = scan.nextDouble();

    System.out.println("Please enter an integer value");
    String e = scan.next(); // String S should be capital..
    String f = scan.next();

    System.out.println("Your integer is: " + a + " " + b + ", your real number is: " + c + " " + d
            + " and your string is: " + e + "  " + f); // here you used wrong variables
   }
}

如果您的问题仍然不清楚,请告诉我您实际卡在哪里。

【讨论】:

    【解决方案2】:

    有几个问题。

    (1) 您需要为您的程序声明一个“入口”点。在 Java 中,您必须创建具有确切签名的方法:

    public static void main(String args) {
    // Your code here
    }
    

    (2)Java中的“string”类型大写。

    (3) 您正在引用既未声明也未定义的变量:

    System.out.println("Your integer is: " + intValue + ", your real number is: "
                                + decimalValue + " and your string is: " + textValue);
    

    在这种情况下,您从未告诉 Java intValue 等的值是什么。似乎您想使用已声明和定义的变量,如下所示:

    System.out.println("Your integer is: " + a + ", your real number is: "
                                + c + " and your string is: " + e);
    

    (4) 看起来您正在为每个提示读取两组变量。根据您的提示“请输入...”,您确实希望输入一个。

    总的来说,我认为您的代码应该如下所示:

    class VariableExample { 
        public static void main(String args) {
            Scanner scan = new Scanner(System.in);
    
            System.out.println("Please enter an integer value: ");
            int a = scan.nextInt();
    
            System.out.println("Please enter a double value: ");
            double c = scan.nextDouble();
    
            System.out.println("Please enter a string: ");
            String e = scan.next();
    
            System.out.println("Your integer is: " + a + ", your real number is: "
                                + c + " and your string is: " + e);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-12-21
      • 1970-01-01
      • 2017-04-07
      • 2021-05-30
      • 2018-04-18
      • 2011-09-24
      • 1970-01-01
      • 2019-11-16
      • 2019-05-23
      相关资源
      最近更新 更多