第五章 迴圈結構課後總結
一.迴圈結構
1.while迴圈結構:
while( 迴圈條件 ){
迴圈操作內容
}
- do while 迴圈結構:
跟while 結構反過來,先寫操作內容,再寫迴圈條件
do{
迴圈操作內容
}
while( 迴圈條件 )
二.課後作業
1.計算100以內的偶數之和
package zyl;
public class Demo {
public static void main(String[] args) {
int num = 2;
int num2 = 0;
while(num<=100){
num2=num2+num;
num=num+2;
System.out.println("偶數的總數和是:"+num2); }
}}
2.升級購物結算
package ZYL;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
int bianhao = 0;
int shuliang = 0;
String name ="";
double je = 0.0;
double hj = 0.0;
String a =“y”;
System.out.println("MyShopping管理系統>購物結算"); System.out.println("***************************************************"); System.out.println("請選擇購買的商品編號:"); System.out.println("1.T恤 2.網球鞋 3.網球拍"); System.out.println("***************************************************"); Scanner scanner = new Scanner(System.in); while("y".equals(a)){ System.out.print("請輸入產品編號:"); bianhao = scanner.nextInt(); switch(bianhao){ case 1: name = "T恤"; je = 245.0; break; case 2: name = "網球鞋"; je =455.0; break; case 3: name = "網球拍"; je = 635.0; break; default: System.out.println("沒有此選項"); } System.out.print("請輸入產品數量:"); shuliang = scanner.nextInt(); System.out.println(name+je+" "+"數量"+shuliang+" "+shuliang*je); hj = hj+shuliang*je; System.out.print("是否繼續(y/n)"); a = scanner.next(); while("y".equals(a)){System.out.print("請輸入產品編號:"); bianhao = scanner.nextInt(); switch(bianhao){ case 1: name = "T恤"; je = 245.0; break; case 2: name = "網球鞋"; je =455.0; break; case 3: name = "網球拍"; je = 635.0; break; default: System.out.println("沒有此選項"); } System.out.print("請輸入產品數量:"); shuliang = scanner.nextInt(); System.out.println(name+je+" "+"數量"+shuliang+" "+shuliang*je); hj = hj+shuliang*je; System.out.print("是否繼續(y/n)"); a = scanner.next(); } } System.out.println("折扣0.8"); System.out.println("應付金額:"+hj*0.8); System.out.print("實付金額:"); double yingfu = scanner.nextDouble(); System.out.println("找錢:"+(yingfu-hj*0.8)); } }
3.攝氏度和華氏度
package ZYL;
public class Demo2 {
public static void main(String[] args) {
double centigrade = 0;
double fahrenheit = 0;
int tiaomu = 1;
do{ centigrade = centigrade+20; fahrenheit = centigrade*9/5.0+32; System.out.println("攝氏度是:"+centigrade+"等於華氏度:"+fahrenheit); tiaomu++;} while(tiaomu<=10&¢igrade<=250); }
}
4.升級選單切換
package ZYL;
import java.util.Scanner;
public class Demo3 {
public static void main(String[] args) {
System.out.println("歡迎使用Myshopping管理系統");
System.out.println("");
System.out.println("*********************************************************");
System.out.println("1.客戶資訊管理");
System.out.println("2.購物結算");
System.out.println("3.真情回饋");
System.out.println("4.登出");
System.out.println("*********************************************************");
System.out.println("");
Scanner scanner = new Scanner(System.in);
System.out.println("請選擇,輸入數字:");
int figure= scanner.nextInt();
do{
System.out.println("輸入錯誤,請重新輸入:");
figure=scanner.nextInt();
} while(figure>4);
System.out.println("執行購物結算");
System.out.println("");
System.out.println("程式結束");
5.教材第三章習題
1.package ZYL;
import java.util.Scanner;
public class Demo4 {
public static void main(String[] args) {
int figure;
Scanner scanner = new Scanner(System.in);
do{
System.out.print("請輸入一個整數(輸入0結束):");
figure = scanner.nextInt();
} while(figure>0);
System.out.print("最大值是:");
int a =scanner.nextInt();
System.out.print("最小值是:");
int b =scanner.nextInt();
}
}
package ZYL;
import java.util.Scanner;
public class Demo5 {
public static void main(String[] args) {
int figure;
Scanner scanner =new Scanner(System.in);
do {
System.out.print(“請輸入數字1-7(輸入0結束):”);
figure = scanner.nextInt();
switch(figure){
case 1:
System.out.println("今天表示MON");
break;
case 2:
System.out.println("今天表示TUE");
break;
case 3:
System.out.println("今天表示WED");
break;
case 4:
System.out.println("今天表示THU");
break;
case 5:
System.out.println("今天表示FRI");
break;
case 6:
System.out.println("今天表示SAT");
break;
case 7:
System.out.println("今天表示SUN");
} } while(figure>0) ;
System.out.println("程式結束");
}
}
3.
package ZYL;
import java.util.Scanner;
public class Demo6 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("請輸入您出行的身份:1-12:");
int time = scanner.nextInt();
if (time <= 12 && time > 10) {
System.out.print("請選擇您選擇頭等艙還是經濟艙?頭等艙輸入1,經濟艙輸入2:");
int room = scanner.nextInt();
if (room == 1) {
System.out.println("您的機票價格為:" + 5000 * 0.5);
}
if (room == 2) {
System.out.println("您的機票價格為:" + 5000 * 0.4);
}
}
if (time <= 10 && time >= 4) {
System.out.print("請選擇您選擇頭等艙還是經濟艙?頭等艙輸入1,經濟艙輸入2:");
int room = scanner.nextInt();
if (room == 1) {
System.out.println("您的機票價格為:" + 5000 * 0.9);
}
if (room == 2) {
System.out.println("您的機票價格為:" + 5000 * 0.6);
}
}
if (time < 4) {
System.out.print("請選擇您選擇頭等艙還是經濟艙?頭等艙輸入1,經濟艙輸入2:");
int room = scanner.nextInt();
if (room == 1) {
System.out.println("您的機票價格為:" + 5000 * 0.5);
}
if (room == 2) {
System.out.println("您的機票價格為:" + 5000 * 0.4);
}
}
}
}
相關推薦
第五章 迴圈結構課後總結
一.迴圈結構 1.while迴圈結構: while( 迴圈條件 ){ 迴圈操作內容 } do while 迴圈結構: 跟while 結構反過來,先寫操作內容,再寫迴圈條件 do{ 迴圈操作內容 } while( 迴圈條件 ) 二.課後作業 1.計
Java第五章迴圈結構
知識要點 一: while迴圈語句格式; 1.初始化表示式.1 2.while(布林表示式.2){ 迴圈體.3 步進表示式.4 } 執行流程: 1.執行順序:1.2.3.4>2.3.4>2.3.4…2 不滿足為止。 2. 1負責完成迴
第五章迴圈結構(一)
一、while迴圈: 1.語法:while(迴圈條件){ //迴圈操作 } 2.迴圈條件是一個bolean型別的結果。 3.特點:先判斷,後執行! 二、程式除錯: 1.步驟: 設定斷點 單步執行 觀察變數 三、 1.i+=2 等價於i=i+2; 2.i++ 等價於i=i+1; 3.sum
第五章 迴圈結構Ⅱ
for迴圈 for( 表示式1; 表示式2; 表示式3){ 迴圈體 } 表示式1是迴圈結構的初始化部分,通常用於定義迴圈變數並賦初始值;
java第五章迴圈課後作業
package com.Chapter.demo;import java.util.Scanner;// /*// * 第五章課後練習,求50以內7的倍數之和;// * 2018.3.30// *///public class Chapter5test4 {////
第五章.面向對象.總結
面向過程編程 多繼承 stat 面向對象編程 隨著 set 但是 結合 優點 面向對象編程 1、面向過程編程 核心是”過程“二字,過程指的是解決問題的步驟,即先幹什麽再幹什麽 基於該思想編寫程序就好比在編寫一條流水線,是一種機械式的思維方式 優點:復
《C++ Primer Plus》學習筆記——第五章 迴圈和關係表示式(一)
本章內容: for迴圈和while迴圈 表示式和語句 運算子組合 複合語句 逗號運算子以及關係運算符 typedef工具 字元輸入方法get() 檔案尾條件 巢狀迴圈和二維陣列 計算機除了儲存資料外,還可以做很多其他工作。可以對資料進
演算法競賽入門經典第2版 第2章 迴圈結構
學習目標 掌握for迴圈、while迴圈、do-while迴圈的使用方法 學會使用計數器和累加器 學會用輸出中間結果的方法除錯 學會用計時函式測試程式效率 學會用重定向、fopen的方式讀寫檔案 瞭解演算法競賽對檔案讀寫方式和命名的嚴格性 記住變數在賦值之前的值是不確
《C++ Primer Plus》學習筆記——第五章 迴圈和關係表示式(二)
while迴圈 while迴圈是沒有初始化和更新部分的for迴圈,它只有測試條件和迴圈體。 while (test-condition) { body; } //當test-condition為true,即不為0時,執行迴圈體。 編寫延時迴圈 long wai
《C++ Primer Plus》學習筆記——第五章 迴圈和關係表示式(三)
迴圈與文字輸入 迴圈完成的一項最常見最重要的任務:逐字元地讀取來自檔案或鍵盤的文字。 cin物件支援三種不同模式的單字元輸入,其使用者介面各不相同。 使用原始的cin進行輸入: 通過迴圈來讀取鍵盤的文字輸入,則必須有辦法停止讀取。 一種方法是選擇某個特殊的字元(哨兵
《C++ Primer Plus》學習筆記——第五章 迴圈和關係表示式(四)
程式設計練習 1.編寫一個要求使用者輸入兩個整數的程式。該程式將計算並輸出這兩個整數之間(包括這兩個整數)所有整數的和。這裡假設先輸入較小的整數。例如,如果使用者輸入的是2和9,則程式將指出2~9之間的所有整數的和為44. #include <iostream>
第六章:迴圈結構(二)
第一題:package cn.bdqn.demo;import java.util.Scanner;public class Test1 {public static void main(String[] args) {int sum = 0; //學習總時間
C++ primer Plus(第六版)中文版 第五章 迴圈和關係表示式 程式設計練習答案
第五章 程式設計練習 1. 編寫一個要求使用者輸入兩個整數的程式。該程式將計算並輸出兩個整數之間(包括這兩個整數)所有整數的和。 這裡假設先輸入較小的整數。例如:如果使用者輸入的是2和9,則程式將指出2~9 之間所有的整數的和為44. 1.1 for 迴圈版 #incl
第三章迴圈結構練習
1.賭骰子: 1 package com.java; 2 3 import java.util.Scanner; 4 5 public class DuBo2 { 6 public static void main(String[
資訊學奧賽一本通(C++版)第一部分 C++語言 第四章 迴圈結構的程式設計
第四章 迴圈結構的程式設計 第一節 for語句 T1059 : 求平均年齡 時間限制: 1000 ms 記憶體限制: 65536 KB 【題目描述】 班上有學生若干名,給出每名學生的年齡(整數),求班上所有學生的平均年齡,保留到小數點後兩位。 【輸入】
中文程式碼示例視訊演示Python入門教程第五章 資料結構
知乎原鏈 續前作: [中文程式碼示例視訊演示Python入門教程第四章 控制流]({{ site.baseurl }}{% link _posts/2018-06-20-中文程式碼示例視訊演示Python入門教程第四章 控制流.markdown %}) 對應線上文件: 5. Data
資訊學奧賽一本通(C++版) 第一部分 C++語言 第四章 迴圈結構的程式設計
//1091 求階乘的和#include <stdio.h>int f(int n){ int i,ans=1; for(i=1;i<=n;i++) ans*=i; return ans;}int main(){ int n,i,sum=0; sc
第八章 迴圈結構進階
1,列印數字public class Num { public static void main(String[] args){ Scanner input = new Scanner(System.in); System.out.print("請輸入行數:")
計算機網路教程第五章運輸層課後習題答案
第五章 傳輸層5—01 試說明運輸層在協議棧中的地位和作用,運輸層的通訊和網路層的通訊有什麼重要區別?為什麼運輸層是必不可少的?答:運輸層處於面向通訊部分的最高層,同時也是使用者功能中的最低層,向它上面的應用層提供服務 運輸層為應用程序之間提供端到
第八章:迴圈結構進階
第一題:編寫程式,列印如下圖案,要求該圖案的行數由使用者輸入。import java.util.Scanner;public class Test {public static void main(String[] args) {Scanner input=new Scann