【发布时间】:2015-02-05 00:08:56
【问题描述】:
我不确定我离结束还有多远,但我必须使用堆栈在 Java 中创建一个程序。我已经完成了 Stack 类,现在唯一给我带来问题的是 Converter 类。我在编译时得到以下信息:
Base10Converter.java:13: cannot find symbol
symbol : variable rand
location: class Base10Converter
System.out.println(outputInBinary());
^
1 error
我知道它不应该是“rand”,所以它只是一个占位符。需要帮助!
import java.util.Scanner;
public class Base10Converter
{
public static void Main(String args[])
{
Scanner scan = new Scanner(System.in);
System.out.println("Please enter your base10 number: ");
String in = scan.nextLine(); //read the value
for (int x = 0; x < in.length(); x++)
{
if (in.charAt(x) >= 0 && in.charAt(x) <= 9)
{
System.out.println(outputInBinary(rand));
}
else
{
System.out.println("Invalid Number! 0 - 9. No letters.");
}
}
}//main
public static void outputInBinary(int in)
{
CharStack cStack = new CharStack();
while(in > 0)
{
int bit = in % 2;
cStack.push((char)(bit + 48));
if (cStack.isFull())
{
System.out.println("Stack is full!");
}
in = in/2;
}
while (!cStack.isEmpty())
{
System.out.println(cStack.pop());
}
}//ouputInBinary
}
编辑:我把“rand”去掉,因为这不是我想要的(我有一个白痴时刻)。我要打印的是第二种方法中弹出的结果。我希望能够打印用户输入的数字的二进制。
【问题讨论】:
-
你想要的随机数的范围是多少?
-
你为什么不用nextInt