對在一個之后放置另一個 if-else 陳述句的位置有點困惑。就像我把它放在 if 陳述句下(“你注冊投票了嗎?”)還是我把它放在 else 陳述句下?
我希望它回答如果是,那么它會列印出“你可以投票”,如果不是,那么它會列印出“你必須先注冊才能投票”
這是代碼
import java.util.Scanner;
public class voting {
public static void main(String[] args) {
int yourage, votersage;
votersage = 18;
Scanner input = new Scanner(System.in);
System.out.println("How old are you? ");
yourage = input.nextInt();
if (yourage >= votersage) {
System.out.println("Have you registered to vote?");
}
else {
System.out.println("You are too young to vote");
}
}
}
uj5u.com熱心網友回復:
我認為這適用于您想要的方式。您可能需要更改 input'Registered 因為我對輸入有點生疏,但我認為這應該可行嗎?
if (yourage >= votersage) {
Scanner input = new Scanner(System.in)
System.out.println("Have you registered to vote?");
Registered = input.next();
if Registered = ("Yes") {
System.out.println("You can vote");
}
else if Registered = ("No"){
System.out.println("You need to register to vote");
}
else:
System.out.println("INVALID INPUT")
}
else {
System.out.println("You are too young to vote");
}
uj5u.com熱心網友回復:
對于 elif 陳述句,您將 elif 放在 else 之前,但請確保為 elif 陳述句添加一個子句,以便像處理原始 if 陳述句一樣運行。
if (yourage >= votersage) {
System.out.println("Have you registered to vote?");
}
else if (yourage <= votersage){
System.out.println("You must register before you can vote.");
}
else {
System.out.println("You are too young to vote");
}
uj5u.com熱心網友回復:
Nested if statement work in these scenarios...
if (yourage >= votersage)
{
System.out.println("Have you registered to vote?");
bool registered = input.next();
if(registered)
{
System.out.println("You can vote");
}
else
{
System.out.println("Please get yourself register on voting portal!");
}
}
else {
System.out.println("You are too young to vote");
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/371056.html
標籤:爪哇 if 语句 java.util.scanner
下一篇:基本的JavaHiLow猜謎游戲
