首先,我很抱歉我的英语不好。 :其中 我正在一个小型的Java项目中工作。我试图创建一个程序来管理学生列表。有两个班。
学生班
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()中的循环仍在运行且不可停止。哪里错了?我该如何解决? :(
答案 0 :(得分:0)
您正在循环中运行递归。如果你弄清楚这个行块是什么意思......你已经解决了你的问题。选择一个或另一个...递归或循环。
int cont = update.nextInt();
if (cont==1) {loop = true;} else {loop=false;}
while (loop=true) {UpdateInfo();}
break;