題目的要求是,要輸入名字,然後選擇繼不繼續輸入!
然後輸入股價,計算出最大和最小值!
但現在問題是,每輸入一個股價,就直接跑出結果.....無法算平均值阿!
import java.util.Scanner;
public class homework
{
public static void main(String arg[])
{Scanner scan = new Scanner(System.in);
Scanner keyboard=new Scanner(System.in);
String name;
String highname=null, lowname=null;
double avg;
double price;
String cont = "yes";
double lowprice = 0;
double highprice = 0;
System.out.print("Enter user name : ");
name = keyboard.nextLine();
while (true){
System.out.println("Do you want to continue? yes/no");
String keyin = scan.next();
if (keyin.equals("YES") || keyin.equals("yes"))
{
System.out.println("Enter stock name: ");
String stock = keyboard.nextLine();
System.out.print("Enter stock price: ");
price = keyboard.nextDouble();
if ( price>= highprice){
highprice=price;
highname=stock;
if ( price<= lowprice){
lowprice=price;
lowname=stock;
}
else if (lowprice==0){
lowprice=price;
lowname=stock;}
//System.out.println("continue? type in yes/no");
keyboard.nextLine();
cont = keyboard.nextLine();
cont= cont.toLowerCase();
}
}
avg=highprice+lowprice/2;
System.out.println("Hello "+name+" , your highest stock was "+highname+"
with price "+highprice+".");
System.out.println("Your lowest stock was "+lowname+" with price
"+lowprice+".");
System.out.println("Thank you for you input, the average of the two
stocks was "+avg+" .");
}
}}
--
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 204.52.215.1