[英]return loop in an if statement
您能幫我提供這個代碼嗎?
任何建議都將不勝感激-Newbie2Java我有很多寶貴意見-謝謝(缺少鏈接)
這是我的代碼:
導入java.util.Scanner;
公共課程Question5WIP {
/**
* @param args
public static void main(String[] args) {
// TODO Auto-generated method stub
// boolean correctInput = false; // assume we dont have correct input
// while (!correctInput){ // continue as long as not correct input
int hourValue = 0;
Scanner in;
while (!(hourValue > 0 && hourValue <= 12)) {
System. out.println("Please enter the hours (between 1 and 12): ");
in = new Scanner(System.in); // User input hour value.
hourValue = in.nextInt();
if (!(hourValue > 0 && hourValue <= 12)) {
System.out.println("Hour Value should be between 1 and 12, please try again "); // invalid hours.
}
}
int minuteValue = 0;
// Scanner in;
while (!(minuteValue > 0 && hourValue <= 60)) {
System. out.println("Please enter the minutes (between 1 and 60): ");
in = new Scanner(System.in); // User input hour value.
minuteValue = in.nextInt();
if (!(minuteValue > 0 && minuteValue <= 60)) {
System.out.println("Invalid entry, please try again: "); // invalid hours.
return;
}
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int hourValue, minuteValue;
Scanner in;
while (true) {
System. out.println("Please enter the hours (between 1 and 12): ");
in = new Scanner(System.in); // User input hour value.
hourValue = in.nextInt();
if (hourValue < 0 || hourValue > 12))
System.out.println("Hour Value should be between 1 and 12, please try again ");
else
break;
}
while(true) {
System.out.println("Please enter the minutes (between 1 and 60): ");
in = new Scanner(System.in);
minuteValue = in.nextInt();
if (minuteValue < 0 || minuteValue > 60))
System.out.println("Minute Value should be between 0 and 60,please try again ");//invalid minutes.
else
break;
}
}
}
用while循環來做! 像這樣:
int hourValue;
while(!(hourValue > 0 && hourValue <= 12)) {
System.out.println("Please enter the hours (between 1 and 12): "); //The program prompts for the hour.
Scanner in = new Scanner(System.in); // User input hour value.
hourValue = in.nextInt();
if(!(hourValue > 0 && hourValue <= 12)){
System.out.println("Hour Value should be between 1 and 12, please try again "); //invalid hours.
return;
}
}
int minuteValue;
System.out.println("Please enter the minutes (between 1 and 60): "); //The program prompts for the hour.
in = new Scanner(System.in); // User input hour value.
minuteValue = in.nextInt();
if(!(minuteValue >= 0 && minuteValue <= 60)){
System.out.println("Minute Value should be between 0 and 60, please try again "); //invalid minutes.
return;
現在,您只需要在幾分鍾內執行相同操作即可。
試試這個它給出正確的答案
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int hourValue = 0;
Scanner in;
while (!(hourValue > 0 && hourValue <= 12)) {
System. out.println("Please enter the hours (between 1 and 12): ");
in = new Scanner(System.in); // User input hour value.
hourValue = in.nextInt();
if (!(hourValue > 0 && hourValue <= 12)) {
System.out.println("Hour Value should be between 1 and 12,
please try again "); // invalid hours.
}
}
int minuteValue;
System.out.println("Please enter the minutes (between 1 and 60): ");
// The program prompts for the hour.
in = new Scanner(System.in); // User input hour value.
minuteValue = in.nextInt();
if (!(minuteValue >= 0 && minuteValue <= 60)) {
System.out.println("Minute Value should be between 0 and 60,
please try again "); // invalid minutes.
return;
}
}
}
導入java.util.Scanner;
公開課測試{
公共靜態void main(String [] args){
int hourValue = 0;
Scanner in;
while (!(hourValue > 0 && hourValue <= 12)) {
System. out.println("Please enter the hours (between 1 and 12): ");
in = new Scanner(System.in); // User input hour value.
hourValue = in.nextInt();
if (!(hourValue > 0 && hourValue <= 12)) {
System.out.println("Hour Value should be between 1 and 12,
please try again "); // invalid hours.
}
}
int minuteValue;
System.out.println("Please enter the minutes (between 1 and 60): ");
// The program prompts for the hour.
in = new Scanner(System.in); // User input hour value.
minuteValue = in.nextInt();
if (!(minuteValue >= 0 && minuteValue <= 60)) {
System.out.println("Minute Value should be between 0 and 60,
please try again "); // invalid minutes.
return;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.