我应该提示用户根据我选择的颜色选择笔的颜色。我已经包含了if,else if,else stamtement和.equalsignorecase,但是每当我运行文件时,它都不让我输入任何东西?
(如果有帮助,我的变量"color“也被定义为一个字符串。)
编辑:我已经在代码中嵌入了一个scanner类。它是:
Scanner cool=new Scanner(system.in)因此,我给我的扫描器起的名字就是其中包含"cool“的任何代码。
System.out.println("Please enter the color that you wish to use to draw your point(color options: Red, blue, light blue, pink, green, orange, magenta, and yellow):");
color=cool.nextLine();
StdDraw.setPenRadius(0.05);
StdDraw.circle(2, 2, 4);
if ("red".equalsIgnoreCase(color))
{StdDraw.setPenColor(StdDraw.RED);}
else if ("blue".equalsIgnoreCase(color))
{StdDraw.setPenColor(StdDraw.BLUE);}
else if ("light blue".equalsIgnoreCase(color))
{StdDraw.setPenColor(StdDraw.BOOK_LIGHT_BLUE);}
else if ("pink".equalsIgnoreCase(color))
{StdDraw.setPenColor(StdDraw.PINK);}
else if ("green".equalsIgnoreCase(color))
{StdDraw.setPenColor(StdDraw.GREEN);}
else if ("orange".equalsIgnoreCase(color))
{StdDraw.setPenColor(StdDraw.ORANGE);}
else if ("magenta".equalsIgnoreCase(color))
{StdDraw.setPenColor(StdDraw.MAGENTA);}
else if ("yellow".equalsIgnoreCase(color))
{StdDraw.setPenColor(StdDraw.YELLOW);}
else
{
StdDraw.setPenColor(StdDraw.CYAN);
}发布于 2017-11-16 11:45:08
您需要有一个Scanner来读取输入。试试这个。
public class Test {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in); // Reading from System.in
System.out.println(
"Please enter the color that you wish to use to draw your point(color options: Red, blue, light blue, pink, green, orange, magenta, and yellow):");
String color = reader.nextLine(); // Scans the next token of the input as an string.
System.out.println(color);
// once finished
reader.close();
}
}发布于 2017-11-16 11:56:28
根据您的问题,假设您希望显示您输入的内容。下面是一些相关的输入过程:
您可以根据需要使用以下任一选项。
扫描器类
import java.util.Scanner;
Scanner scan = new Scanner(System.in);
String s = scan.next();
int i = scan.nextInt();BufferedReader和InputStreamReader类
import java.io.BufferedReader;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
int i = Integer.parseInt(br.readLine());DataInputStream类
import java.io.DataInputStream;
DataInputStream dis = new DataInputStream(System.in);
int i = dis.readInt();DataInputStream类中的readLine方法已弃用。要获取字符串值,应使用前面的BufferedReader解决方案
控制台类
import java.io.Console;
Console console = System.console();
String s = console.readLine();
int i = Integer.parseInt(console.readLine());显然,这种方法在某些IDE中不能很好地工作。您接受的输入可以由System.out.println(input_object)打印/提示。
https://stackoverflow.com/questions/47321148
复制相似问题