如何正确使用 Switch-Case?
How can I use the Switch-Case in the right way?
首先,我很抱歉我的英语不好。 :<
我在一个小型 Java 项目中工作。我试图创建一个程序来管理学生名单。有两个 class。
学生class
import java.util.Scanner;
public class Student {
String rollNo;
String name;
int age;
String address;
float tMark;
float eMark;
public String getRollNo() {
return rollNo;
}
public void setRollNo(String rollNo) {
this.rollNo = rollNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public float gettMark() {
return tMark;
}
public void settMark(float tMark) {
this.tMark = tMark;
}
public float geteMark() {
return eMark;
}
public void seteMark(float eMark) {
this.eMark = eMark;
}
public void EnterInfo() {
Scanner in = new Scanner(System.in);
System.out.println("\nEnter the number: ");
setRollNo(in.nextLine());
System.out.println("Enter the name: ");
setName(in.nextLine());
System.out.println("Enter the age: ");
setAge(in.nextInt());
System.out.println("Enter the address: ");
setAddress(in.nextLine());
System.out.println("Enter the Theory mark: ");
settMark(in.nextFloat());
System.out.println("Enter the Exam mark: ");
seteMark(in.nextFloat());
System.out.println("\n");
}
public void UpdateInfo() {
Scanner update = new Scanner(System.in);
int choice = 0;
boolean loop = false;
System.out.println("Select a field to update: ");
System.out.println("1. Number: " + getRollNo());
System.out.println("2. Name: " + getName());
System.out.println("3. Age: " + getAge());
System.out.println("4. Address: " + getAddress());
System.out.println("5. Theory mark: " + gettMark());
System.out.println("6. Exam mark: " + geteMark());
choice = update.nextInt();
switch (choice) {
case 1:
System.out.println("New number: ");
setRollNo(update.nextLine());
break;
case 2:
{System.out.println("New name: ");
setName(update.nextLine());}
case 3:
{System.out.println("New age: ");
setAge(update.nextInt());}
case 4:
{System.out.println("New address: ");
setAddress(update.nextLine());}
case 5:
{System.out.println("New Theory mark: ");
settMark(update.nextFloat());}
case 6:
{System.out.println("New Exam mark: ");
seteMark(update.nextFloat());}
case 7:
System.out.println("Do you want to continue updating? (1.Yes/2.No): ");
int cont = update.nextInt();
if (cont==1) {loop = true;} else {loop=false;}
while (loop=true) {UpdateInfo();}
break;
}
}
public void StudentInfo() {
System.out.println("===\nNo.: " + getRollNo() + "\nName: " + getName()
+ "\nAge: " + getAge() + "\nAddress: " + getAddress()
+ "\nT.Mark: " + gettMark() + "\nE.Mark: " + geteMark()
+ "\n===");
}
}
和 MainClass(用于测试目的)
public class MainClass {
public void showMenu() {
System.out.println("1. Enter the students info.");
System.out.println("2. Show list of student.");
System.out.println("3. Find info.");
System.out.println("4. Update info");
System.out.println("5. Exit!");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Student std = new Student();
std.UpdateInfo();
}
}
不幸的是,我的程序终于出错了。 :(
我无法用我的英语解释这个问题。但总的来说,我无法更新变量中的内容,UpdateInfo() 中的循环仍然 运行 并且无法停止。什么问题?我该如何解决? :(
你是运行循环中的递归。如果您弄清楚此行块的含义......您已经解决了您的问题。选择一个或另一个...递归或循环。
int cont = update.nextInt();
if (cont==1) {loop = true;} else {loop=false;}
while (loop=true) {UpdateInfo();}
break;
首先,我很抱歉我的英语不好。 :< 我在一个小型 Java 项目中工作。我试图创建一个程序来管理学生名单。有两个 class。
学生class
import java.util.Scanner;
public class Student {
String rollNo;
String name;
int age;
String address;
float tMark;
float eMark;
public String getRollNo() {
return rollNo;
}
public void setRollNo(String rollNo) {
this.rollNo = rollNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public float gettMark() {
return tMark;
}
public void settMark(float tMark) {
this.tMark = tMark;
}
public float geteMark() {
return eMark;
}
public void seteMark(float eMark) {
this.eMark = eMark;
}
public void EnterInfo() {
Scanner in = new Scanner(System.in);
System.out.println("\nEnter the number: ");
setRollNo(in.nextLine());
System.out.println("Enter the name: ");
setName(in.nextLine());
System.out.println("Enter the age: ");
setAge(in.nextInt());
System.out.println("Enter the address: ");
setAddress(in.nextLine());
System.out.println("Enter the Theory mark: ");
settMark(in.nextFloat());
System.out.println("Enter the Exam mark: ");
seteMark(in.nextFloat());
System.out.println("\n");
}
public void UpdateInfo() {
Scanner update = new Scanner(System.in);
int choice = 0;
boolean loop = false;
System.out.println("Select a field to update: ");
System.out.println("1. Number: " + getRollNo());
System.out.println("2. Name: " + getName());
System.out.println("3. Age: " + getAge());
System.out.println("4. Address: " + getAddress());
System.out.println("5. Theory mark: " + gettMark());
System.out.println("6. Exam mark: " + geteMark());
choice = update.nextInt();
switch (choice) {
case 1:
System.out.println("New number: ");
setRollNo(update.nextLine());
break;
case 2:
{System.out.println("New name: ");
setName(update.nextLine());}
case 3:
{System.out.println("New age: ");
setAge(update.nextInt());}
case 4:
{System.out.println("New address: ");
setAddress(update.nextLine());}
case 5:
{System.out.println("New Theory mark: ");
settMark(update.nextFloat());}
case 6:
{System.out.println("New Exam mark: ");
seteMark(update.nextFloat());}
case 7:
System.out.println("Do you want to continue updating? (1.Yes/2.No): ");
int cont = update.nextInt();
if (cont==1) {loop = true;} else {loop=false;}
while (loop=true) {UpdateInfo();}
break;
}
}
public void StudentInfo() {
System.out.println("===\nNo.: " + getRollNo() + "\nName: " + getName()
+ "\nAge: " + getAge() + "\nAddress: " + getAddress()
+ "\nT.Mark: " + gettMark() + "\nE.Mark: " + geteMark()
+ "\n===");
}
}
和 MainClass(用于测试目的)
public class MainClass {
public void showMenu() {
System.out.println("1. Enter the students info.");
System.out.println("2. Show list of student.");
System.out.println("3. Find info.");
System.out.println("4. Update info");
System.out.println("5. Exit!");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Student std = new Student();
std.UpdateInfo();
}
}
不幸的是,我的程序终于出错了。 :( 我无法用我的英语解释这个问题。但总的来说,我无法更新变量中的内容,UpdateInfo() 中的循环仍然 运行 并且无法停止。什么问题?我该如何解决? :(
你是运行循环中的递归。如果您弄清楚此行块的含义......您已经解决了您的问题。选择一个或另一个...递归或循环。
int cont = update.nextInt();
if (cont==1) {loop = true;} else {loop=false;}
while (loop=true) {UpdateInfo();}
break;