我有下面的類提供用戶輸入:Java類
class Area
{
//Get User Input for classes
int length;
int width;
public Area(int x,int y)
{
length = x;
width = y;
}
public int getArea() {
return width * length;
}
public static void main(String[] args)
{
Area folk = new Area(4,5);
System.out.println("Area of 4 * 5 is: " + folk.getArea());
}
}
我有我用它來獲取用戶輸入的另一個類:
import java.util.Scanner;
class Incoming
{
public static void main(String[] args)
{
Scanner reader = new Scanner(System.in);
System.out.println("Enter the first number");
//get user input for a
int a = reader.nextInt();
System.out.println("Input Value Is: " + a);
}
}
在第一類中,我想用戶提供輸入而不是預定義的值(即Area folk = new Area(4,5)
)
這怎麼能做到?
um,..也許用用戶輸入的int也可以是構造函數的參數。換句話說,創建兩個int變量a和b,使用Scanner獲取用戶輸入,然後使用a和b調用Area構造函數。 – 2012-04-07 13:52:25
你可以在這裏閱讀關於它的詳細教程:http://www.cs.utexas.edu/users/ndale/Scanner.html – dexametason 2012-04-07 13:55:16
感謝氣墊船充滿鰻魚,工作。 – Gandalf 2012-04-07 14:01:32