小编Fer*_*et9的帖子

设置对象的颜色

我在我的AP计算机科学课中使用GridWorld,我们正在做的一部分是改变bug(一个对象)的颜色.我找到了一个非常基本的方法来做这个,但我想尝试合并字符串,以允许用户输入他想要的bug颜色,而不必输入RGB的数字值.通过输入"red"并使字符串存储,我能够将字符串值作为我想要的颜色.但是如何将该字符串转换为颜色呢?我不确定我是否说得足够清楚,但是我附上了我的代码,所以希望有人会理解并且可以提供帮助.

Color red = new Color (255, 0, 0);
Color green = new Color (0, 255, 0);
Color blue = new Color (0, 0, 255);

System.out.println("What color would you like the first bug to be? (red, green, blue)");
String name = "color1";
String color1 = keyboard.next();

if (color1 == "red")
{
   world.add (new Location (bugx1, bugy1), new Bug(red));
}
if (color1 == "blue")
{
   world.add (new Location (bugx1, bugy1), new Bug(blue));
}
if (color1 == "green")
{
   world.add (new Location …
Run Code Online (Sandbox Code Playgroud)

java string gridworld

6
推荐指数
1
解决办法
3401
查看次数

未使用局部变量

我试图测试输入的单词是否是回文(相同的拼写向前和向后).从我可以看到它应该工作,但Eclipse说"不使用局部变量isPalindrome的值",但它被使用.问题在于,即使这个词不是回文,它也是如此.

import java.util.Scanner;

public class Palindrome {
    public static void main(String[] args) {
        String phrase;
        char[] phraseLetters;
        int endChar;
        boolean isPalindrome;

        Scanner input = new Scanner(System.in);
        System.out.println("Enter a word or phrase.");
        phrase = input.nextLine();
        input.close();

        phrase = phrase.toLowerCase();
        phrase = phrase.replaceAll(" ","");
        phraseLetters = phrase.toCharArray();

        endChar = phraseLetters.length - 1;

        for (int i = 0; i < phraseLetters.length; i++) {
            if (phraseLetters[i] != phraseLetters[endChar]) {
                isPalindrome = false;   
            } else {
                isPalindrome = true;
                endChar -= 1;
            }
        }

        if …
Run Code Online (Sandbox Code Playgroud)

java eclipse local-variables palindrome

0
推荐指数
1
解决办法
3612
查看次数

标签 统计

java ×2

eclipse ×1

gridworld ×1

local-variables ×1

palindrome ×1

string ×1