gpt4 book ai didi

java - 异常处理和字符串转换器

转载 作者:行者123 更新时间:2023-12-01 06:08:38 25 4
gpt4 key购买 nike

下面是我的代码,尚未完成,

import java.lang.* ;
import java.util.Scanner;

public class pbtwashing
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
String satu,dua,tiga,empat,weightrange,message,minus,rinse,spin,d,comp;
int x,min;
double a;
minus="\t\t -----------------------------------------------------------------------------";
System.out.println("\n\n\n\t\t ----------------------------------------------------------------------------");
System.out.println("\t\t| Welcome To PBU Laundry ! |");
System.out.println("\t\t| There are 4 type of wash you can choose. |");
System.out.println("\t\t| -----------------------------------------------------------------------------|");
System.out.println("\t\t| a-Normal Wash | a. 2kg == 30 Mins | b. 3kg == 40 Mins | c. 5kg == 50 Mins |");
System.out.println("\t\t| | |");
System.out.println("\t\t| -----------------------------------------------------------------------------|");
System.out.println("\t\t| b-Hard Wash | a. 2kg == 40 Mins | b. 3kg == 50 Mins | c. 5kg == 60 Mins |");
System.out.println("\t\t| | |");
System.out.println("\t\t| -----------------------------------------------------------------------------|");
System.out.println("\t\t| c-Delicate Wash | a. 2kg == 30 Mins | b. 3kg == 40 Mins | c. 5kg == 50 Mins |");
System.out.println("\t\t| | (Rinse after wash step will take extra 3 seconds) |");
System.out.println("\t\t| -----------------------------------------------------------------------------|");
System.out.println("\t\t| d-Speed Wash | a. 2kg == 30 Mins | b. 3kg == 40 Mins |");
System.out.println("\t\t| | (Cloths weight cannot more than 3kg) |");
System.out.println("\t\t -----------------------------------------------------------------------------");
System.out.print("\t\t\n\n Please choose between (1-4) : ");

min=0;
d="\n\n\t\t\tdone\n\n";
message="\n\n\t\t\tStep 1 : Washing......\n\n";
rinse="\n\n\t\t\tStep 2 : Rinsing......\n\n";
spin="\n\n\t\t\tStep 3 : Spining......\n\n";
comp="\n\n\t\t\tyour laundry is now complete\n\n\t\t\tminutes taken for laundry to complete is : " ;
satu="Normal Wash";
dua="Hard Wash";
tiga="Delicate Wash";
empat="Speed Wash";
weightrange =" Please choose your cloth weight range";

x = in.nextInt();
if (x == 1)
{
System.out.println("\n\n\t\t\t\t\t" + satu + " has been Chosen." + "\n" +minus);
System.out.print("\t\t\t" + weightrange + " (2kg/3kg/5kg) : ");

min=in.nextInt();
switch (min)
{
case 2:
System.out.println(minus);
slowPrint(message, 100);
tenminutes();
slowPrint3(d,100);
slowPrint1(rinse, 100);
tenminutes();
slowPrint3(d,100);
slowPrint2(spin, 100);
tenminutes();
slowPrint3(d,100);
slowPrint4(comp,100);
a=30;
System.out.print(a);
System.out.println("\n"+minus);
break;
case 3:
System.out.println(minus);
slowPrint(message, 100);
thirteenminutes();
slowPrint3(d,100);
slowPrint1(rinse, 100);
thirteenminutes();
slowPrint3(d,100);
slowPrint2(spin, 100);
thirteenminutes();
slowPrint3(d,100);
slowPrint4(comp,100);
a=40;
System.out.print(a);
System.out.println("\n"+minus);
break;
case 5:
System.out.println(minus);
slowPrint(message, 100);
sixteenminutes();
slowPrint3(d,100);
slowPrint1(rinse, 100);
sixteenminutes();
slowPrint3(d,100);
slowPrint2(spin, 100);
sixteenminutes();
slowPrint3(d,100);
slowPrint4(comp,100);
a=50;
System.out.print(a);
System.out.println("\n"+minus);
break;
default:
System.out.println("\n"+minus);
System.out.print("\n\n\t\t\t\t\t\tWrong Input ");

}
}
else if (x == 2)
{
System.out.println("\n\n\t\t\t\t\t" + dua + " has been Chosen." + "\n" +minus);
System.out.print("\t\t\t" + weightrange + " (2kg/3kg/5kg) : ");
min = in.nextInt();

switch (min)
{
case 2:
System.out.println(minus);
slowPrint(message, 100);
thirteenminutes();
slowPrint3(d,100);
slowPrint1(rinse, 100);
thirteenminutes();
slowPrint3(d,100);
slowPrint2(spin, 100);
thirteenminutes();
slowPrint3(d,100);
slowPrint4(comp,100);
a=30;
System.out.print(a);
System.out.println("\n"+minus);
break;
case 3:
System.out.println(minus);
slowPrint(message, 100);
sixteenminutes();
slowPrint3(d,100);
slowPrint1(rinse, 100);
sixteenminutes();
slowPrint3(d,100);
slowPrint2(spin, 100);
sixteenminutes();
slowPrint3(d,100);
slowPrint4(comp,100);
a=50;
System.out.print(a);
System.out.println("\n"+minus);
break;
case 5:
System.out.println(minus);
slowPrint(message, 100);
twentyminutes();
slowPrint3(d,100);
slowPrint1(rinse, 100);
twentyminutes();
slowPrint3(d,100);
slowPrint2(spin, 100);
twentyminutes();
slowPrint3(d,100);
slowPrint4(comp,100);
a=60;
System.out.print(a);
System.out.println("\n"+minus);
break;
default:
System.out.println("\n"+minus);
System.out.print("\n\n\t\t\t\t\t\tWrong Input ");
break;
}
}
else if (x == 3)
{
System.out.println("\n\n\t\t\t\t\t" + tiga + " has been Chosen." + "\n" +minus);
System.out.print("\t\t\t" + weightrange + " (2kg/3kg/5kg) : ");

min=in.nextInt();

switch (min)
{
case 2:
System.out.println(minus);
slowPrint(message, 100);
tenminutes();
slowPrint3(d,100);
slowPrint1(rinse, 100);
tenminutes();
slowPrint3(d,100);
slowPrint2(spin, 100);
tenminutes();
slowPrint3(d,100);
slowPrint4(comp,100);
a=30;
System.out.print(a);
System.out.println("\n"+minus);
break;
case 3:
System.out.println(minus);
slowPrint(message, 100);
thirteenminutes();
slowPrint3(d,100);
slowPrint1(rinse, 100);
thirteenminutes();
slowPrint3(d,100);
slowPrint2(spin, 100);
thirteenminutes();
slowPrint3(d,100);
slowPrint4(comp,100);
a=40;
System.out.print(a);
System.out.println("\n"+minus);
break;
case 5:
System.out.println(minus);
slowPrint(message, 100);
sixteenminutes();
slowPrint3(d,100);
slowPrint1(rinse, 100);
sixteenminutes();
slowPrint3(d,100);
slowPrint2(spin, 100);
sixteenminutes();
slowPrint3(d,100);
slowPrint4(comp,100);
a=50;
System.out.print(a);
System.out.println("\n"+minus);
break;
default:
System.out.println("\n"+minus);
System.out.print("\n\n\t\t\t\t\t\tWrong Input ");
}
}
else if (x == 4)
{
System.out.println("\n\n\t\t\t\t\t" + empat + " has been Chosen." + "\n" +minus);
System.out.print("\t\t\t" + weightrange + " (2kg/3kg/5kg) : ");
min=in.nextInt();
switch (min)
{
case 2:
System.out.println(minus);
slowPrint(message, 100);
tenminutes();
slowPrint3(d,100);
slowPrint1(rinse, 100);
tenminutes();
slowPrint3(d,100);
slowPrint2(spin, 100);
tenminutes();
slowPrint3(d,100);
slowPrint4(comp,100);
a=30;
System.out.print(a);
System.out.println("\n"+minus);
break;
case 3:
System.out.println(minus);
slowPrint(message, 100);
thirteenminutes();
slowPrint3(d,100);
slowPrint1(rinse, 100);
thirteenminutes();
slowPrint3(d,100);
slowPrint2(spin, 100);
thirteenminutes();
slowPrint3(d,100);
slowPrint4(comp,100);
a=40;
System.out.print(a);
System.out.println("\n"+minus);
break;
default:
System.out.println("\n"+minus);
System.out.print("\n\n\t\t\t\t\t\tWrong Input ");
}
}


else
{
System.out.println("\n"+minus);
System.out.print("\n\n\t\t\t\t\t\tWrong Input ");
}
}

static void tenminutes()
{
for ( int i = 0; i <= 10; i++ )
{
System.out.println("\t\t\t"+ i + " minutes ");
}
}

static void thirteenminutes()
{
for ( int i = 0; i <= 13; i++ )
{
System.out.println("\t\t\t"+ i + " minutes ");
}
}

static void sixteenminutes()
{
for ( int i = 0; i <= 16; i++ )
{
System.out.println("\t\t\t"+ i + " minutes ");
}
}


static void twentyminutes()
{
for ( int i = 0; i <= 20; i++ )
{
System.out.println("\t\t\t"+ i + " minutes ");
}
}

/**
* Function to print each character in a string with a delay (a "typewriter" effect)
* @param message The string to print
* @param millisPerChar Milliseconds to take to print each character
*/
public static void slowPrint(String message, long millisPerChar)
{
for (int i = 0; i < message.length(); i++)
{
System.out.print(message.charAt(i));
try
{
Thread.sleep(millisPerChar);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
/**
* Function to print each character in a string with a delay (a "typewriter" effect)
* @param String The string to print
* @param millisPerChar Milliseconds to take to print each character
*/
public static void slowPrint1(String rinse, long millisPerChar)
{
for (int i = 0; i < rinse.length(); i++)
{
System.out.print(rinse.charAt(i));

try
{
Thread.sleep(millisPerChar);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}

}
/**
* Function to print each character in a string with a delay (a "typewriter" effect)
* @param spin The string to print
* @param millisPerChar Milliseconds to take to print each character
*/
public static void slowPrint2(String spin, long millisPerChar)
{
for (int i = 0; i < spin.length(); i++)
{
System.out.print(spin.charAt(i));

try
{
Thread.sleep(millisPerChar);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
public static void slowPrint3(String d, long millisPerChar)
{
for (int i = 0; i < d.length(); i++)
{
System.out.print(d.charAt(i));

try
{
Thread.sleep(millisPerChar);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}

}
public static void slowPrint4(String comp, long millisPerChar)
{
for (int i = 0; i < comp.length(); i++)
{
System.out.print(comp.charAt(i));

try
{
Thread.sleep(millisPerChar);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}

谁能帮我申请NumberFormatException在布料重量范围内,如果用户不满足输入要求,它将循环,我还需要将字符串转换为原始数据。我已经尝试了很多次但仍然无法完成。预先感谢您的任何帮助。ps/抱歉我的英语不好。

最佳答案

注意:你的代码太长,我没看懂。

我有点明白你想做的事。您希望不断要求用户输入,直到输入有效。我希望我正确理解了你的意思。

正如您所说,执行此操作的一种方法是使用 NumberFormatException :

int userInput;
Scanner scan = new Scanner(System.in);
while(true) {
try {
userInput = Integer.parseInt(scan.nextLine());
break;
} catch (NumberFormatException e) {
System.out.println("Input Invalid! Try again!");
}
}
// do stuff with userInput

但是,不建议使用异常作为控制执行的方式,因为抛出异常需要花费大量时间。

检查输入是否有效的另一种方法是使用正则表达式:

final Pattern pattern = Pattern.compile(<<Regular Expression Here>>);
int userInput;
String userString;
Scanner scan = new Scanner(System.in);
while(true) {
userString = scan.nextLine();
if (pattern.matcher(userString).matches()) {
break;
} else {
System.out.println("Input invalid. Try again!");
}
}
userInput = Integer.parseInt(userString);

看到<<Regular Expression Here>>东西?如果您希望用户输入无符号整数,请将其替换为 "\\d+" 。如果您希望用户输入有符号整数,请将其替换为 "[\\+-]?\\d+" .

关于java - 异常处理和字符串转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39425364/

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