# 新手想請問JAVA的程式設計!

You will write a short program that will keep track of the highest and lowest value stocks that a user may enter.

The program will first ask the user for their name. Then you will ask for a stock name followed by a stock price(do not expect an integer input).

Then you will ask the user if they wish to continue. If the user types in yes(your program should handle any capitalization, Yes, YES, etc) you will continue asking for name and price pairs. If the user types in no, (again, handle any capitalization) you will stop the program and output the following three lines of text.

Hello [Name], your highest stock was [stockhigh] with price [pricehigh].

Your lowest stock was [stocklow] with price [pricelow].

Thank you for you input, the average of the two stocks was [average].

### 2 個解答

• 8 年前
最佳解答

請參考我的做法

import java.util.*;

public class Y1022 {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.print("Name: ");

String name = scan.nextLine();

String highStockName = null, lowStockName = null;

double highStockPrice = Double.MIN_VALUE, lowStockPrice = Double.MAX_VALUE;

while (true) {

System.out.print("Stock Name: ");

String sn = scan.nextLine();

System.out.print("Stock Price: ");

double d = Double.parseDouble(scan.nextLine());

if (d > highStockPrice) {

highStockPrice = d;

highStockName = sn;

}

if (d < lowStockPrice) {

lowStockPrice = d;

lowStockName = sn;

}

System.out.print("Continue? ");

String con = scan.nextLine();

if (con.toLowerCase().equals("yes")) continue;

if (con.toLowerCase().equals("no")) break;

}

System.out.println("Hello " + name + ", your highest stock was " +

highStockName +" with price " + highStockPrice + ".");

System.out.println("Your lowest stock was " + lowStockName +

" with price " + lowStockPrice + ".");

System.out.println("Thank you for you input, the average of the two " +

"stocks was " + (highStockPrice+lowStockPrice)/2.0D + ".");

}

}

• 8 年前

大概長這個樣子...

http://pastie.org/4771220