在我的情况1中,当用户决定停止存储任何项目时,我试图读取输入。用户按回车键,我希望程序返回菜单部分。此外,我想在案件结束后每次都返回菜单。我该怎么办?
import java.text.*;
import java.util.*;
public class MonthlyExpenditure {
static Scanner input= new Scanner(System.in).useDelimiter("\r\n");
static DecimalFormat fmt=new DecimalFormat("0.00");
public static void main(String[] args )
{
int choice, month=0,i=0,j=month ;
String b;
double[] totalAmount= new double[12];
String[][] item= new String [12][11]; //12= column(month), 11=row(item)
double[][] amount= new double[12][11]; //12=column(month), 11=row(amount)
System.out.println("************* Expenditure ***************");
System.out.println("1> Enter monthly expenses");
System.out.println("2> Display detailed expenditure by month");
System.out.println("3> Quick glance at monthly expenses");
System.out.println("4> Exit");
System.out.print("Please select your choice <1-4>");
choice=input.nextInt();
System.out.println("*****************************************");
switch(choice)
{
case 1 :
System.out.print("Enter month <1 for Jan - 12 for Dec>: ");
month=input.nextInt();
b=getMonth(month);
System.out.println(b+" expenditure <max 10 items>");
while(i<10){ //repeat asking user to enter up to 10 item
j=month;
System.out.print("Enter item "+(i+1)+" <Press ENTER to exit> : "); //"while" to only prompt enter item and amount
item[j][i]=input.next();
if(item[j][i]==" ")
System.out.println("meunu");
System.out.print("Enter amount : ");
amount[j][i]=input.nextDouble();
i++;
}
break;
case 2 :
System.out.print("Enter month <1 for Jan - 12 for Dec>: ");
month=input.nextInt();
b=getMonth(month);
j=month;
System.out.println("Expenditure for "+b);
i=0;
while(item[j][i]!=null)
{ System.out.println(item[j][i]+"\t\t\t"+amount[j][i]);
i++;
}
break;
case 3 :
System.out.println("Monthly expenditure :");
System.out.println(" ");
{
b=getMonth(month);
j=month;
totalAmount[j]=amount[j][0]+amount[j][1]+amount[j][2]+amount[j][3]+amount[j][4]+amount[j][5]+amount[j][6]+amount[j][7]+amount[j][8]+amount[j][9];
System.out.print(b+"\t\t$"+totalAmount[j]);
if(totalAmount[j]>2500)
System.out.println("\t\t\t over spent!!");
else
System.out.println(" ");
}
break;
case 4 :
System.out.println("Thank you for using this System");
System.exit(0);
break;
default :
System.out.println("Error. Enter only 1-4");
break;
}while(choice<1||choice>4);
}
public static String getMonth(int b)
{
String month=" ";
switch (b) {
case 1:month="Jan";
break;
case 2:month="Feb";
break;
case 3:month="Mar";
break;
case 4:month="Apr";
break;
case 5:month="May";
break;
case 6:month="Jun";
break;
case 7:month="Jul";
break;
case 8:month="Aug";
break;
case 9:month="Sep";
break;
case 10:month="Oct";
break;
case 11:month="Nov";
break;
case 12:month="Dec";
break;
default: System.out.println("Invalid number. Only 1-12 is recognisable.");
}//end switch
return month;}
答案 0 :(得分:1)
在这种情况下不要使用nextInt
,而是nextLine()
,然后使用Integer.parseInt()
进行解析。
答案 1 :(得分:0)
如果您想允许用户在switch语句中的操作内的任何位置取消,您需要首先使用Scanner#nextLine()
读取他们的输入,然后测试该行是否等于空 - 串。如果是的话,就爆发了。
String line = input.nextLine();
if (line.equals(""))
{
break; // Go to next-line after inner-most loop/switch structure
}
// Code for basic course of events
要再次将用户返回到菜单,您可以将代码的相关部分(即:从打印菜单的位置到switch语句的末尾)包装在某种类似的循环中。
do {
// Your code to print out the menu and perform operations here
} while (choice != 4); // Replace 4 with whatever value associated with an exit command
答案 2 :(得分:0)
一个简单的解决方案就是将所有内容包装在while(true)循环中。您可以将菜单创建字符串移动到静态方法,只需在需要再次显示菜单时调用该方法。
这样的事情应该对createMenu方法有用,你可能想要处理用户不输入整数的情况。
private static int createMenu() {
System.out.println("************* Expenditure ***************");
System.out.println("1> Enter monthly expenses");
System.out.println("2> Display detailed expenditure by month");
System.out.println("3> Quick glance at monthly expenses");
System.out.println("4> Exit");
System.out.print("Please select your choice <1-4>: ");
int choice = Integer.parseInt(input.nextLine());
System.out.println("*****************************************");
return choice;
}
你的主要方法可以这样开始:
public static void main(String[] args) {
int choice, month = 0, i = 0, j = month;
String b;
double[] totalAmount = new double[12];
String[][] item = new String[12][11]; //12= column(month), 11=row(item)
double[][] amount = new double[12][11]; //12=column(month), 11=row(amount)
while (true) {
choice = createMenu();
switch (choice) {
// etc
当用户点击返回键时,您可以突破内部while循环:
if (item[j][i].isEmpty()) {
break;
}