gpt4 book ai didi

java - 如何让这段代码继续循环,直到输入正确的学号?

转载 作者:行者123 更新时间:2023-12-01 10:50:20 27 4
gpt4 key购买 nike

如何让此代码继续询问“学生编号”,直到正确输入以字符“X”开头的学生编号,然后继续执行该程序?

import java.util.Scanner;
public class SDevCA2 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here

//inputs
//Option 1


String userName;
String passWord1;
String passWord2;
String eol;
String userInput;
int loopVal =0;
char firstLetter = 'X';
int userOption;
final double FULLGRANT = 3000;
final double GRANTSEVENTYFIVE = 2250;
final double GRANTFIFTY = 1500;

double softWareGrade =0;
double mathsGrade =0;
double systemGrade =0;
double computerArchGrade =0;


double gradeCount;
final double SUBJECT_COUNT = 4;

double averageGrade;
int overallAverageResults;
int feesOwedPercent;
double feesOwedEuros;

//Option 2

double feesPaidByGrant =0;
double feesNotPaidByGrant =0;
int totalStudentsProcessed =0;
int totalStudentsRecievedGrant =0;

//Option 3

int studentsWith100Paid =0;
int studentsWith75Paid =0;
int studentsWith50Paid =0;
int studentsWithNoGrant =0;

int categoryMostStudents;

//End of Inputs
boolean run = true;
while ( run ) {
Scanner in = new Scanner (System.in);

//input of data
//input of data

System.out.println("Please enter your username: ");
userName = in.nextLine();
System.out.print("Please enter your password: ");
passWord1 = in.nextLine();
System.out.print("Please re-enter your password: ");
passWord2 = in.nextLine();
while (!passWord1.equals(passWord2))
{

System.out.println("Incorrect! Please try re-entering password ");
System.out.print("Please enter your password: ");
passWord1 = in.nextLine();
System.out.print("Please re-enter your password: ");
passWord2 = in.nextLine();

}


//ABC Grant Menu.
System.out.println("\n --------------------------------");
System.out.println ("Welcome to the ABC College Grant System \n 1. Calculate Grant \n 2. Fee Statistics \n 3. Grant Category Information \n 4. Exit");
userOption = in.nextInt();


//if Invalid Entry Error Message with the ABC Menu again



//Option 1
switch ( userOption ) {
case 1:


String studentName;
String studentNumber;



System.out.println ("Please Enter your Student Name: ");
studentName = in.next();




System.out.println ("Please Enter your Student Number: ");
userInput = in.next();

while ( userInput.charAt(0) == firstLetter) {
System.out.println("Correct");
break;

}

while ( userInput.charAt(0) != firstLetter)
{


System.out.println("No match, Student Numbr must begin with character capital X ");
}






{





//Error message if Student Number doesn't start with X

System.out.println ("What was your Grade in Software Development");
softWareGrade = in.nextDouble();

System.out.println ("What was your Grade in Maths");
mathsGrade = in.nextDouble ();

System.out.println("What was your Grade in Systems Analasys");
systemGrade = in.nextDouble ();

System.out.println("What was your Grade in Computer Archetecture");
computerArchGrade = in.nextDouble();


//Formula

gradeCount = softWareGrade + mathsGrade + systemGrade + computerArchGrade;
averageGrade = gradeCount / SUBJECT_COUNT;

//100% FEES PAID





if (averageGrade >=80 && averageGrade <=100) {
feesOwedPercent = 0;
feesOwedEuros = 0;
totalStudentsProcessed ++;
totalStudentsRecievedGrant ++;
studentsWith100Paid ++;
feesPaidByGrant = feesPaidByGrant + FULLGRANT;
System.out.println ("Student Name: " + studentName);
System.out.println ("Student Number: " + userInput);
System.out.println ("Average Grade: " +averageGrade);
System.out.println("Fees Owed: " +feesOwedPercent);
System.out.println("Fees not paid " + feesOwedEuros);

}

if (averageGrade >=60 && averageGrade <80 ) {
feesOwedPercent = 25;
feesOwedEuros = 750;
totalStudentsProcessed ++;
totalStudentsRecievedGrant ++;
studentsWith75Paid ++;
feesPaidByGrant = feesPaidByGrant + GRANTSEVENTYFIVE;
feesNotPaidByGrant = feesNotPaidByGrant + feesOwedEuros;
System.out.println ("Student Name: " + studentName);
System.out.println ("Student Number: " + userInput);
System.out.println ("Average Grade" +averageGrade);
System.out.println("Fees Owed: " +feesOwedPercent);
System.out.println("Fees not paid " + feesOwedEuros );
}

if (averageGrade >=40 && averageGrade <60) {
feesOwedPercent = 50;
feesOwedEuros = 1500;
totalStudentsProcessed ++;
totalStudentsRecievedGrant ++;
studentsWith50Paid ++;
feesPaidByGrant = feesPaidByGrant + GRANTFIFTY;
feesNotPaidByGrant = feesNotPaidByGrant + feesOwedEuros;
System.out.println ("Student Name: " + studentName);
System.out.println ("Student Number: " + userInput);
System.out.println ("Average Grade" +averageGrade);
System.out.println("Fees Owed: " +feesOwedPercent);
System.out.println("Fees not paid " + feesOwedEuros );


}

if ( averageGrade <40 ) {
feesOwedPercent = 100;
feesOwedEuros = 3000;
totalStudentsProcessed ++;
studentsWithNoGrant ++;
feesNotPaidByGrant = feesNotPaidByGrant + feesOwedEuros;
System.out.println ("Student Name: " + studentName);
System.out.println ("Student Number: " + userInput);
System.out.println ("Average Grade" +averageGrade);
System.out.println("Fees Owed: " +feesOwedPercent);
System.out.println("Fees not paid " + feesOwedEuros );
break;

}

switch ( userOption) {
case 2:
System.out.println("The overall fess paid by the grant: €" +feesPaidByGrant );
System.out.println("The overall fees not paid by the grant: €" +feesNotPaidByGrant );
System.out.println("The total number of students processed is: " +totalStudentsProcessed );
System.out.println("The total number of students who recieved a grant is: " +totalStudentsRecievedGrant );
break;

}
switch ( userOption ) {
case 3:
System.out.print("How many stduents are given grants in the following catergory");
System.out.println("100% Paid: " +studentsWith100Paid );
System.out.println("75% Paid: " +studentsWith75Paid );
System.out.println("50% Paid: " +studentsWith50Paid );
System.out.println("No Grant Paid: " +studentsWithNoGrant );
break;
}

switch ( userOption) {
case 4:
System.out.print("The number of stduents that are given grants in the following catergories: ");
System.out.println("\n");
System.out.println("100% Paid: " +studentsWith100Paid );
System.out.println("75% Paid: " +studentsWith75Paid );
System.out.println("50% Paid: " +studentsWith50Paid );
System.out.println("No Grant Paid: " +studentsWithNoGrant );

System.out.print("\n");
System.out.print("\n");
System.out.print("The catergory from which most grants are paid is: ");





run=false;
break;
default:
}

}
}
}
}
}

最佳答案

提示用户输入学号然后接受输入的代码必须位于 while 循环内。尝试这样的事情:

boolean validNumber = false;
while (!validNumber) {
System.out.println ("Please Enter your Student Number: ");
userInput = in.next();

if (userInput.charAt(0) == firstLetter) {
validNumber = true;
} else {
System.out.println("No match, Student Numbr must begin with character capital X ");
}
}

关于java - 如何让这段代码继续循环,直到输入正确的学号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33958523/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com