循环的 switch case 在 Java 中:

huangapple go评论77阅读模式
英文:

Looping switch case in java

问题

System.out.print("输入等级代码:");
levelCode = input.next().charAt(0);

do {

    switch (levelCode) {
        case 'F':
            System.out.print("基础班学生总数 = ");
            studentFoundation = input.nextInt();
            foundationCash = studentFoundation * 50;
            break;
        case 'D':
            System.out.print("文凭班学生总数 = ");
            studentDiploma = input.nextInt();
            diplomaCash = studentDiploma * 50;
            break;
        case 'B':
            System.out.print("学士班学生总数 = ");
            studentBachelor = input.nextInt();
            bachelorCash = studentBachelor * 70;
            break;
        case 'M':
            System.out.print("硕士班学生总数 = ");
            studentMaster = input.nextInt();
            masterCash = studentMaster * 90;
            break;
        case 'P':
            System.out.println("博士班学生总数 = ");
            studentPhd = input.nextInt();
            phdCash = studentPhd * 90;
            break;
        default:
            System.out.println("无效的代码");
            break;

    }
} while (levelCode < 5); //我不知道应该在这里放什么以便循环执行switch case

totalCash = foundationCash + diplomaCash + bachelorCash + masterCash + phdCash; //在这里我想计算所有输入的金额

System.out.println("总现金金额 = " + totalCash);
英文:

I'm currently working on the code that will prompt student level type in code (switch case), and to determine the amount of cash for each type of student will receive. Then I want to sum all the cash received by each student level. I am fairly a beginner and have only learn java for a few weeks, any suggestion is very appreciated. Here is what I have worked on, there are also notes.

System.out.print(&quot;Level code :&quot;);
levelCode = input.next().charAt(0);

do {

    switch (levelCode) {
        case &#39;F&#39;:
            System.out.print(&quot;Total Foundation student = &quot;);
            studentFoundation = input.nextInt();
            foundationCash = studentFoundation * 50;
            break;
        case &#39;D&#39;:
            System.out.print(&quot;Total Diploma student = &quot;);
            studentDiploma = input.nextInt();
            diplomaCash = studentDiploma * 50;
            break;
        case &#39;B&#39;:
            System.out.print(&quot;Total Bachelor student = &quot;);
            studentBachelor = input.nextInt();
            bachelorCash = studentBachelor * 70;
            break;
        case &#39;M&#39;:
            System.out.print(&quot;Total Master student = &quot;);
            studentMaster = input.nextInt();
            masterCash = studentMaster * 90;
            break;
        case &#39;P&#39;:
            System.out.println(&quot;Total PhD student = &quot;);
            studentPhd = input.nextInt();
            phdCash = studentPhd * 90;
            break;
        default:
            System.out.println(&quot;Invalid code&quot;);
            break;

    }
} while (levelCode &lt; 5); //i dont know what to put here in order to loop the switch case

totalCash = foundationCash + diplomaCash + bachelorCash + masterCash + phdCash; //here i want to calculate all the input entered

System.out.println(&quot;Total cash amount = &quot; + totalCash);

答案1

得分: 1

假设您只想在执行default情况时使循环回退,可以通过不同的方式实现。

使用标签

LOOP: for (;;) { // 无限循环
    System.out.print("等级代码:");
    levelCode = input.next().charAt(0);

    switch (levelCode) {
        case 'F':
            // 在此处编写代码
            break LOOP; // <=== 跳出循环,而不是switch语句
        case 'D':
            // 在此处编写代码
            break LOOP; // <=== 跳出循环,而不是switch语句
        ...
        default:
            // 在此处编写代码
    }
}

使用布尔值

boolean error;
do {
    System.out.print("等级代码:");
    levelCode = input.next().charAt(0);

    error = false;
    switch (levelCode) {
        case 'F':
            // 在此处编写代码
            break;
        case 'D':
            // 在此处编写代码
            break;
        ...
        default:
            error = true;
            // 在此处编写代码
    }
} while (error);

使用特殊值,例如null

String levelCode;
do {
    System.out.print("等级代码:");
    levelCode = input.next();

    switch (levelCode) {
        case "F":
            // 在此处编写代码
            break;
        case "D":
            // 在此处编写代码
            break;
        ...
        default:
            // 在此处编写代码
            levelCode = null;
    }
} while (levelCode == null);
英文:

Assuming you only want the loop to go back if the default case is executed, you can do it in different ways.

Using a label

LOOP: for (;;) { // forever loop
    System.out.print(&quot;Level code :&quot;);
    levelCode = input.next().charAt(0);

    switch (levelCode) {
        case &#39;F&#39;:
            // code here
            break LOOP; // &lt;=== break out of the loop, not the switch statement
        case &#39;D&#39;:
            // code here
            break LOOP; // &lt;=== break out of the loop, not the switch statement
        ...
        default:
            // code here
    }
}

Using a boolean

boolean error;
do {
    System.out.print(&quot;Level code :&quot;);
    levelCode = input.next().charAt(0);

    error = false;
    switch (levelCode) {
        case &#39;F&#39;:
            // code here
            break;
        case &#39;D&#39;:
            // code here
            break;
        ...
        default:
            error = true;
            // code here
    }
} while (error);

Using a special value, e.g. null

String levelCode;
do {
    System.out.print(&quot;Level code :&quot;);
    levelCode = input.next();

    switch (levelCode) {
        case &quot;F&quot;:
            // code here
            break;
        case &quot;D&quot;:
            // code here
            break;
        ...
        default:
            // code here
            levelCode = null;
    }
} while (levelCode == null);

答案2

得分: 0

我认为一个简单的解决方案是首先提示用户输入学生的数量,然后将您的 switch 语句放在一个 for 循环中。

英文:

I think a simple solution would be to first prompt the user to type how many students there are and then put your switch statement in a for loop.

答案3

得分: 0

LOOP: for (;;){
switch(levelCode){
case 'F' :
System.out.print("Total Foundation student = ");
studentFoundation = input.nextInt();
foundationCash = studentFoundation * 50;

        System.out.print("Level code :");
        levelCode = input.next().charAt(0);
        break;
        
    case 'D' : 
        System.out.print("Total Diploma student = ");
        studentDiploma = input.nextInt();
        diplomaCash = studentDiploma * 50;
        
        System.out.print("Level code :");
        levelCode = input.next().charAt(0);
        break;
        
    case 'B' : 
        System.out.print("Total Bachelor student = ");
        studentBachelor = input.nextInt();
        bachelorCash = studentBachelor * 70;
        
        System.out.print("Level code :");
        levelCode = input.next().charAt(0);
        break;
        
    case 'M' : 
        System.out.print("Total Master student = ");
        studentMaster = input.nextInt();
        masterCash = studentMaster * 90;
        
        System.out.print("Level code :");
        levelCode = input.next().charAt(0);
        break;
        
    case 'P' : 
        System.out.println("Total PhD student = ");
        studentPhd = input.nextInt();
        phdCash = studentPhd * 90;
        break LOOP;
        
    default : 
        System.out.println("Invalid code :");
        break LOOP;
}

}

英文:

So I've tried using the for loop method, thanks to Andreas for giving an example :), and it worked!

Right now I dont know how to prompt user to input code again to continue the loop, but I have typed in a few lines to prompt the code and it works as intended, but Im sure this is not the most efficient way of doing it?

And Im planning to break the loop once user entered the last input/entered an invalid code

Here's the new modified code

LOOP: for (;;){

    switch(levelCode){
     case &#39;F&#39; : System.out.print(&quot;Total Foundation student = &quot;);
                studentFoundation = input.nextInt();
                foundationCash = studentFoundation * 50;
                
                System.out.print(&quot;Level code :&quot;);//here im trying to prompt the user to enter another code to continue the loop
    levelCode = input.next().charAt(0);
    break;
     case &#39;D&#39; : System.out.print(&quot;Total Diploma student = &quot;);
                studentDiploma = input.nextInt();
                diplomaCash = studentDiploma * 50;
                
                System.out.print(&quot;Level code :&quot;);
    levelCode = input.next().charAt(0);
    break;
     case &#39;B&#39; : System.out.print(&quot;Total Bachelor student = &quot;);
                studentBachelor = input.nextInt();
                bachelorCash = studentBachelor * 70;
                
                System.out.print(&quot;Level code :&quot;);
    levelCode = input.next().charAt(0);
    break;
     case &#39;M&#39; : System.out.print(&quot;Total Master student = &quot;);
                studentMaster = input.nextInt();
                masterCash = studentMaster * 90;
                
                System.out.print(&quot;Level code :&quot;);
    levelCode = input.next().charAt(0);
    break;
     case &#39;P&#39; : System.out.println(&quot;Total PhD student = &quot;);
                studentPhd = input.nextInt();
                phdCash = studentPhd * 90;
                break LOOP;
                
      default : System.out.println(&quot;Invalid code :&quot;);
                break LOOP;
                
               
    }      
  }

huangapple
  • 本文由 发表于 2020年10月24日 22:50:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/64514653.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定