我想在Eclipse中用Java创建一个程序,告诉我,如果我可以创建三角形.这是我的代码:
import java.io.IOException;
public class haromszog {
public static void main(String[] args) throws IOException {
int a;
int b;
int c;
System.out.print("Please insert the 'a' side of the triangle:");
a = System.in.read();
System.out.print("Please insert the 'b' side of the triangle:");
b = System.in.read();
System.out.print("Please insert the 'c' side of the triangle:");
c = System.in.read();
if ((a+b)>c)
{
if ((a+c)>b)
{
if ((b+c)>a)
{System.out.print("You can make this triangle");
}
else
System.out.print("You can't make this triangle");
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
Eclipse可以运行它,但它写道:
请插入三角形的'a'侧:(例如我写:):5
请插入三角形的'b'侧:
请插入三角形的"c"侧:
你不能做这个三角形
我不能写任何东西到b和c方面.这有什么问题?
System.in.read()byte从应用程序的标准输入中读取单个内容.这几乎肯定不是你想要的(除非有什么东西将二进制数据传递给你的应用程序).
您可以尝试System.console().readLine()(然后Integer.parseInt()将结果转换String为a int).
来自http://download.oracle.com/javase/1.4.2/docs/api/java/io/InputStream.html:
read():从输入流中读取下一个数据字节.
你读的不是整数,而是char代码.
你应该这样做:
java.util.Scanner s = new java.util.Scanner(System.in);
int k = s.nextInt();
Run Code Online (Sandbox Code Playgroud)