首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >提示用户选择钢笔颜色不起作用

提示用户选择钢笔颜色不起作用
EN

Stack Overflow用户
提问于 2017-11-16 11:36:45
回答 2查看 57关注 0票数 0

我应该提示用户根据我选择的颜色选择笔的颜色。我已经包含了if,else if,else stamtement和.equalsignorecase,但是每当我运行文件时,它都不让我输入任何东西?

(如果有帮助,我的变量"color“也被定义为一个字符串。)

编辑:我已经在代码中嵌入了一个scanner类。它是:

代码语言:javascript
复制
Scanner cool=new Scanner(system.in)

因此,我给我的扫描器起的名字就是其中包含"cool“的任何代码。

代码语言:javascript
复制
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);
}
EN

回答 2

Stack Overflow用户

发布于 2017-11-16 11:45:08

您需要有一个Scanner来读取输入。试试这个。

代码语言:javascript
复制
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();

    }

}
票数 0
EN

Stack Overflow用户

发布于 2017-11-16 11:56:28

根据您的问题,假设您希望显示您输入的内容。下面是一些相关的输入过程:

您可以根据需要使用以下任一选项。

扫描器类

代码语言:javascript
复制
import java.util.Scanner; 
Scanner scan = new Scanner(System.in);
String s = scan.next();
int i = scan.nextInt();

BufferedReader和InputStreamReader类

代码语言:javascript
复制
import java.io.BufferedReader;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
int i = Integer.parseInt(br.readLine());

DataInputStream类

代码语言:javascript
复制
import java.io.DataInputStream;
DataInputStream dis = new DataInputStream(System.in);
int i = dis.readInt();

DataInputStream类中的readLine方法已弃用。要获取字符串值,应使用前面的BufferedReader解决方案

控制台类

代码语言:javascript
复制
import java.io.Console;
Console console = System.console();
String s = console.readLine();
int i = Integer.parseInt(console.readLine());

显然,这种方法在某些IDE中不能很好地工作。您接受的输入可以由System.out.println(input_object)打印/提示。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47321148

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档