㈠ java選擇排序的兩個for循環是什麼意思
這么理解----把內層的for循環 循環外層for循環定義的次數
for1(5次){
for2(10次)
}
for1--循環5次
for2--循環50次
㈡ java中的do while 循環問題
很高興回答你的問題
java中有許多循環(for,while,do while 等等),因為do while至少執行一次的特點,所以在使用過程中一定要控制好,根據自己的需求來選擇循環!
我這里用了while循環來實現:
================================ start ====================================
import java.util.Scanner;
public class TestAdd{
public static void main(String[] args){
int a = 0;
int b = 0;
int c = 0;
StringBuffer message = new StringBuffer();
Scanner scan = new Scanner(System.in);
System.out.println("\t\t歡迎光臨本店");
while(true){
System.out.println("====================================");
System.out.println("1:t恤衫(60元) 2:網球鞋(150元) 3:網球拍(80元)");
System.out.println("註:輸入「N」終止購物,下達訂單!");
System.out.println("====================================");
String readStr = scan.nextLine().trim();
if("1".equals(readStr)){
a++;
}else if("2".equals(readStr)){
b++;
}else if("3".equals(readStr)){
c++;
}else if("N".equals(readStr)){
break;
}else if(!"Y".equals(readStr)){
System.out.println("您輸入不正確,請重新輸入!");
}
}
if(a>0){
message.append("t恤衫:").append(a).append(" 件,共").append(a*60).append("元;");
}
if(b>0){
message.append("網球鞋:").append(b).append(" 雙,共").append(b*150).append("元;");
}
if(c>0){
message.append("網球拍:").append(c).append(" 副,共").append(c*80).append("元。");
}
if(!"".equals(message.toString())){
System.out.println("您此次購物訂單如下:");
System.out.println(message);
System.out.println("總計:"+(a*60+b*150+c*80)+" 元");
}else{
System.out.println("您此次沒有購買任何商品!");
}
System.out.println("==============歡迎下次光臨==============");
}
}
================================ end ====================================
如果滿意,請及時採納,謝謝! 祝學習愉快!