繁体   English   中英

如何在同一级别的if语句中分配/访问变量

[英]How to assign/access variables in if statements on the same level

因此,我需要访问我在最后一个if语句中的if语句中分配的变量。 我是Java的初学者,我们将为您提供帮助:)

这是我的编译错误。

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
The local variable a may not have been initialized
The local variable b may not have been initialized
The local variable c may not have been initialized
The local variable d may not have been initialized
The local variable e may not have been initialized

这是我的代码。 我需要根据经过的迭代次数来分配某个变量,并在最后一次迭代中分配完变量后访问这些变量。

public static void main(String[] args){
    String a;
    String b;
    String c;
    String d;
    String e;
    Scanner s = new Scanner(System.in);

for( int loop = 1; loop <= 6; loop++ ){
    if(loop == 1){
        System.out.println("Please input a grade.");
        a = s.nextLine();
        System.out.println("You said,"+a);
        continue;
        }
    if(loop == 2){
        System.out.println("Please input a grade.");;
        b = s.nextLine();
        System.out.println("You said,"+b);
        continue;
    }
    if(loop == 3){
        System.out.println("Please input a grade.");
        c = s.nextLine();
        System.out.println("You said," +c);
        continue;
    }
    if(loop == 4){
        System.out.println("Please input a grade.");
        d = s.nextLine();
        System.out.println("You said," +d);
        continue;
    }
    if(loop == 5){
        System.out.println("Please input a grade.");
        e = s.nextLine();
        System.out.println("You said," +e);
        continue;
    }
    if(loop == 6){
        System.out.println("Grade 1."+a);
        System.out.println("Grade 1."+b);
        System.out.println("Grade 1."+c);
        System.out.println("Grade 1."+d);
        System.out.println("Grade 1."+e);
    }


    }
}

您正在声明,然后尝试访问字符串。 如果要使用变量,则必须先对其进行初始化。 您可以通过以下方式声明它们:

String a;

您还需要像这样初始化它们:

String a = "";

在尝试访问它们之前初始化它们,否则Java的编译器将不高兴!

实际上,在这种情况下,正确的做法是删除循环和if语句 它们是多余的。 如果您没有多次运行其中的任何操作,则无需执行循环:

String a;
String b;
String c;
String d;
String e;

System.out.println("Please input a grade.");
a = s.nextLine();
System.out.println("You said,"+a);

System.out.println("Please input a grade.");;
b = s.nextLine();
System.out.println("You said,"+b);

System.out.println("Please input a grade.");
c = s.nextLine();
System.out.println("You said," +c);

System.out.println("Please input a grade.");
d = s.nextLine();
System.out.println("You said," +d);

System.out.println("Please input a grade.");
e = s.nextLine();
System.out.println("You said," +e);

System.out.println("Grade 1."+a);
System.out.println("Grade 1."+b);
System.out.println("Grade 1."+c);
System.out.println("Grade 1."+d);
System.out.println("Grade 1."+e);

这将为您提供完全相同的控制流程,但是现在编译器可以看到所有变量都必须已初始化。

循环用于以很小的差异重复运行程序逻辑的某些部分。 实际上,如果您使用数组而不是变量,那么有一个循环是有意义的:

String[] grades = new String[6];

for ( int loop = 0; loop < 6; loop++ ) {

    System.out.println("Please input a grade.");
    grades[loop] = s.nextLine();
    System.out.println("You said," + grades[loop]);

}

for ( int loop = 0; loop < 6; loop++ ) {
    System.out.println( "Grade " + ( loop + 1 ) + ". " + grades[loop] );
}

因此,我们没有写所有这些行六次,而是只写了一次,更改取决于循环变量。 这就是循环的目的。

创建字符串时只需执行以下操作:

String a = "";
String b = "";
String c = "";
String d = "";
String e = "";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM