2016-02-18 30 views
0

我想知道是否有人可以告訴我是否有可能在輸入6個產品輸入或/和6個輸入量的數據後輸入一週中的某幾天。所以基本上程序顯示星期一,然後循環6次,然後到週二循環6次,然後星期三。如果有可能我可以得到一些想法?我真的很感謝一些幫助。我沒有運氣Google搜索,這是因爲我知道我不是在問谷歌正確的問題。通過星期幾環路

import java.util.Scanner; 

     public class Mailorder { 

     public static void main(String[] args) { 

     //create a scanner 
     Scanner input = new Scanner(System.in); 

     //declare variables 

     double product1 = 3.75; 
     double product2 = 5.95; 
     double product3 = 8.75; 
     double product4 = 6.92; 
     double product5 = 8.75; 
     double product6 = 7.87; 
     double sum2 = 0; 
     int sum1 = 0; 
     double total = 0.00; 
     int product; 
     int quantity; 

     //Monday 
     System.out.print("Monday"); 
     System.out.println(); 

     //read in product # 
     System.out.print("Enter a product number: "); 
     product = input.nextInt(); 

     //read in quantity sold 
     System.out.print("Enter quantity sold: "); 
     quantity = input.nextInt(); 


     //keep reading data until the input is 0 
     while (quantity != -1) { 
       sum1 += quantity; 

     //switch case 
     switch (product) 
     { 
     case 1: total = product1 * quantity; break; 
     case 2: total = product2 * quantity; break; 
     case 3: total = product3 * quantity; break; 
     case 4: total = product4 * quantity; break; 
     case 5: total = product5 * quantity; break; 
     case 6: total = product6 * quantity; break; 
     } 

     sum2 +=total; 

     //read the next data 
     System.out.print("Enter a product number: "); 
      product = input.nextInt(); 

     System.out.print("Enter quantity sold: "); 
      quantity = input.nextInt(); 

     } 
     //print results 
     System.out.println("The total retail value of all products sold last week $" + sum2); 

    } 
    } 
+1

您的單詞「loop」正處於正確的軌道上。嘗試搜索「java loop」。 – VGR

+0

如果它不在你的頭上,你可能會從使用枚舉中受益。 – user1803551

+0

您可以使用一個更多來自用戶的輸入,不管它的星期一或星期二等等,然後循環將像現在一樣工作 – Abhishekkumar

回答

1

對於未格式化的文本抱歉,我從移動應答。

我想你需要的是這個操作符「%」

內部while循環,你可以增加一個計數

int count=0; 
String currentDay="Monday"; 
String nextDay; 


// your while loop start here 

count++; 

if(count%6 == 0) 
{ 
    switch(currentDay) 
    { 
    case "Monday": nextDay="Tuesday"; break; 
    //etc 
    } 
currentDay = nextDay; 
} 

%運營商將會給你一個部門的提醒。

+0

謝謝你,似乎更多的是我在找什麼。現在試着讓它起作用。 – jake

+0

我已經添加了這個,但現在我在程序結束時出現錯誤。抱歉的格式,它顯示正確的時候輸入但顯示時是錯誤的//保持讀取數據,直到輸入爲-1 \t \t \t while(quantity!= -1) \t \t \t \t sum1 + = quantity; \t \t \t \t \t \t count ++; \t \t \t if(count%6 == 0); {「{」「);}} //星期二 \t \t \t} \t \t \t如果(計數%5 == 1); { \t \t \t的System.out.println( 「星期三」); // {週三 \t \t \t} – jake

+0

它不是我給了確切的代碼。這是關於如何使用%操作符的想法。我會編輯答案 – Cozimetzer

2

使用內循環:

for(--- each day of the week ---) 
    // do something 

    for(int i=0; i<6; i++) { 
     // do something else 
    } 
} 

要找到是什麼星期幾(如何實現第一環),看看Calendar類:https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#getActualMinimum(int)

int firstDay = Calendar.getInstance().getActualMinimum(Calendar.DAY_OF_WEEK); 
int lastDay = Calendar.getInstance().getActualMaximum(Calendar.DAY_OF_WEEK); 
+0

謝謝你我會研究一下。我很欣賞幫助! – jake

+0

我正在查看日曆,但我無法使用它,因爲我們稍後會繼續。有沒有辦法使用switch語句或使用沒有日曆的循環? – jake

+0

由於Java 8中有一個枚舉['java.time.DayOfWeek'](http://docs.oracle.com/javase/8/docs/api/java/time/DayOfWeek.html),您可以使用它來代替'Calendar'。關於它的好處是,通過使用for-each循環,您的循環變得更簡單。 – siegi

1

DayOfWeek枚舉

DayOfWeek枚舉定義了七個對象,一個星期中的每一天,週一至週日編號1-7。

EnumSet是用於收集枚舉對象的高度優化的Set實現。

EnumSet<DayOfWeek> dows = EnumSet.allOf(DayOfWeek.class); // Collect all seven day-of-week objects, in order Monday-Sunday. 

Loop set。

for(DayOfWeek dow : dows) { 
    String dowName = dow.getDisplayName(TextStyle.FULL , Locale.US) ; // Generate localized string for the name of the day-of-week. 
    System.out.println(dowName) ; 
    … // Ask for product code and do your math 
}