如何将值声明并读入整数变量?

时间:2014-10-25 19:33:38

标签: java

我对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);
}

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

2 个答案:

答案 0 :(得分:0)

你忘了申报入口点,即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
   }
}

如果问题仍未明确,请告诉我您实际停留的位置。

答案 1 :(得分:0)

有几个问题。

(1)您需要声明一个"条目"指向你的程序。在Java中,您必须创建具有确切签名的方法:

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

(2)" string" Java中的type是大写的。

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

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

在这种情况下,你从来没有告诉过javaValue等的值是什么。您似乎想要使用已声明和定义的变量,如:

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);
    }
}