我在程序中创建是/否输入时遇到问题。我希望用户能够在最后一个print语句之后输入y / n,但我不确定如何实现它。我知道它很可能包括布尔值或if / else语句,但我不太确定。有什么帮助吗?
import java.util.*;
import java.io.*;
import java.util.Scanner;
public class Person
{
public static void main(String[]args)
{
int age;
String name;
Scanner scan = new Scanner(System.in);
System.out.println("Enter in your age.");
age = scan.nextInt();
if (age >= 10 && age < 18)
{
System.out.println("So you're a kid, huh?");
}
else if (age < 10)
{
System.out.println("Nice try.");
System.exit(0);
}
else if (age >= 18 && age <= 100)
{
System.out.println("So you're an adult, huh?");
}
else if (age > 100)
{
System.out.println("Nice try.");
System.exit(0);
}
Scanner in = new Scanner(System.in);
System.out.println("Enter in your name");
name = in.nextLine();
System.out.println("So you're " + age + " years old and your name is " + name + "?");
System.out.println("y/n");
}
}
答案 0 :(得分:9)
String answer;
boolean yn;
System.out.println("y/n");
while (true) {
answer = in.nextLine().trim().toLowerCase();
if (answer.equals("y")) {
yn = true;
break;
} else if (answer.equals("n")) {
yn = false;
break;
} else {
System.out.println("Sorry, I didn't catch that. Please answer y/n");
}
}
答案 1 :(得分:3)
这是你需要的吗?
String answer = in.nextLine()
Boolean yn = (answer.equalsIgnoreCase("Y") ? true : false);
或者,您可以使用更短的形式:
Boolean yn = (answer.equalsIgnoreCase("Y"));
如果您有其他意思,请发表澄清。
答案 2 :(得分:2)
只需添加boolean answer = in.nextLine().toLowerCase().startsWith("y");
因此,如果您输入以answer
开头的任何内容,基本上它会将y
设置为true。否则就是假的。
答案 3 :(得分:0)
您将读入一个字符串并将其与文字“y”进行比较。比较产生一个布尔值。
你可以扩展这个以首先检查它是“y”还是“n”并且如果它既不是也不是要接受其他等价物如“是”和“否”则会抱怨。
在任何情况下,您的布尔值都是通过与文字进行比较来实现的。
答案 4 :(得分:0)
试试这个,
boolean answer = "y".equalsIgnoreCase(in.nextLine());
答案 5 :(得分:0)
您可以使用do while loop
,例如:
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int age;
String name;
String input = "y";
do {
Scanner scan = new Scanner(System.in);
System.out.println("Enter in your age.");
age = scan.nextInt();
if (age >= 10 && age < 18) {
System.out.println("So you're a kid, huh?");
}
else if (age < 10) {
System.out.println("Nice try.");
System.exit(0);
} else if (age >= 18 && age <= 100) {
System.out.println("So you're an adult, huh?");
} else if (age > 100) {
System.out.println("Nice try.");
System.exit(0);
}
Scanner in = new Scanner(System.in);
System.out.println("Enter in your name");
name = in.nextLine();
System.out.println("So you're " + age
+ " years old and your name is " + name + "?");
Scanner sc = new Scanner(System.in);
System.out.println("y/n");
input = sc.nextLine();
} while (input.equalsIgnoreCase("y"));
}
}
答案 6 :(得分:0)
我弄清楚了。
import java.util.Scanner;
public class MuseumHours2 {
public static void main(String[] args) {
String day;
boolean yn = true;
boolean userContinue;
String answer;
Scanner stdIn = new Scanner(System.in);
do {
System.out.println("Hello, thank you for visiting the Museum
website. For what day would you like to check our hours of operation?");
day = stdIn.nextLine();
System.out.println("Is today a holiday? Enter y or n.");
answer = stdIn.nextLine();
if (answer.equals("y")) {
yn = true;
} else if (answer.equals("n")) {
yn = false;
} else {
System.out.println("Sorry, I didn't understand. Please reply y or n.");
}
switch (day) {
case "Monday":
System.out.println("Closed.");
break;
case "Tuesday":
if (yn) {
System.out.println("We are open from 1:00 to 4:00.");
} else {
System.out.println("We are open from 12:00 to 5:00.");
}
break;
case "Wednesday":
if (yn) {
System.out.println("We are open from 2:00 to 5:00.");
} else {
System.out.println("We are open from 1:00 to 6:00.");
break;
}
case "Thursday":
System.out.println("Closed.");
break;
}
System.out.println("Do you want to re-run the program? Type y or n.");
} while (userContinue = "y".equalsIgnoreCase(stdIn.nextLine()));
}
}
答案 7 :(得分:0)
这里是一个示例
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
boolean yn;
String ageq;
System.out.println("Please enter your age: ");
ageq = sc.next();
System.out.println("You entered " + ageq + " is this correct?");
System.out.println("y/n");
OUTER:
while (true) {
ageq = sc.next().trim().toLowerCase();
switch (ageq) {
case "y":
yn = true;
break OUTER;
case "n":
yn = false;
break OUTER;
default:
System.out.println("Sorry, I didn't get that. Please enter y/n");
break;
}
}
}
答案 8 :(得分:0)
使用import json
from requests import Session
from bs4 import BeautifulSoup
URL = 'https://fr.trustpilot.com/review/jardiland.com?page=2'
session = Session()
r = session.get(URL)
soup = BeautifulSoup(r.text)
data = soup.find('script',{'type':'application/ld+json'})
data_json = json.loads(data.getText(strip=True))
# now you can assess data as dictionary
,对于“是”和“是”,它将返回true