2016-03-26 21 views
0

Şu anda bilgisayar programlama programımın ilk yarıyılında, üniversitede birinci sınıf öğrenciyim; Aynı anda sözde kod ve Java ile çalışarak başladık.Java: Breaks ile ilgili bir derleme hatası

Kullanıcı tarafından girilen bir dizi sayıyı toplayan ve ortalama oluşturan bir java uygulamasının oluşturulmasıyla görevlendirildik. Kullanıcı, 0 sayısı girilene kadar istediği sayıda sayı girebilir, bu durumda program toplamı ve ortalamayı çıkarır ve daha sonra kullanıcının programdan başlamasını veya programdan çıkmasını ister.

Bu görevi, biri yuvalanmış olmak üzere iki tane kullanarak gerçekleştirdim. Bununla birlikte, iç içe döngüyü kırmaya çalışırken, "Hata: dış anahtar veya devre dışı bırak" hatasını aldım. Daha sonra, bu foruma göz gezdirerek, problemle ilgili cevaplar ve bilgiler arayan çok zaman harcadım, fakat hiçbiri benim sorunumla alakalı değildi ve düzeltmeler işe yaramadı. Listede etiketli aralar kullanıyor ve küme parantezlerini düzeltiyordu; çünkü bunlar işe yaramadı, problemin kodun daha derin olduğuna inanıyorum.

Dersim online olduğu için, profesör veya diğer öğrencilerle zamanında iletişim kurmak çok zordur, bu yüzden bu topluma yöneldim!

Aşağıda profesörün başvurumuzu temel almamızı istediği sözde kodu ekleyeceğim.

Start 
Declarations 
Num sumTotal = 0 // Initialize for clarity 
Num numEntered 
Num averageNum 
Num loopCounter 
Num answer 
String endProgram = “Y” // Initialize so that outer loop will work 
    End declarations 
// Greet the user 
Output “Welcome to our calculator. “ 
Output “Enter as many numbers as you want.” 
Output “When you are done entering numbers, enter 0 (zero) to display the sum.” 
Output “Do you want to start the calculator? (Y/N): “ // Let the user decide to start 
input endProgram 
// Note: if the user enters anything but Y or y, the loop will not execute. 
While endProgram <> “Y” OR endProgram <> “y” // Allows the user to perform multiple calculations 
//Enter the first number (sentinel value) 
Output “Please enter your first number.” 
Input numEntered 
While numEntered <> 0 // Allows the user to enter numbers for the current calculation 
    Output “You entered the number “ + numEntered // echo input 
    sumTotal = sumTotal + numEntered // Add number entered to total 
     loopCounter++ // Increment the number of entries 
Output “Please enter the next number” 
Input numEntered // If 0, the loop will end here 
endWhile // the nested inner loop code stops here 
// Output section 
Output “The total numbers entered is: “ + loopCounter 
Output “The total of the numbers entered is: “ + sumTotal 
Output “The average of the numbers entered is: “ + averageNum 
Output “Would you like to do a new set of calculations? Y/N 
Input endProgram 
    End While // End outer While statement when endProgram = Y 
    Output “Thank you for using the calculator. The program will now end.”  
Stop // Stop the program 

benim Java kodu ben kod özensiz veya garip olabilir anlıyorum

/* Module 4 Assignment 1 Part 1 
* Aaron Field 
* Submitted March 26, 2016 
* Using DrJava */ 

import javax.swing.JOptionPane; 
public class Calculator { 

    public static void main(String[] args) { 

    //variable declarations 
    int numEntered; 
    int sumTotal = 0; 
    int averageNum = sumTotal/numEntered; 
    int loopCounter; 
    String endProgram = "Y"; 
    //end declarations 

    System.out.println("Welcome to the Totaling Calculator"); 
    System.out.println("This program will accept integer inputs until 0 is entered" + '\n' + "When 0 is entered, a sum and an average will be displayed."); 

endProgram = JOptionPane.showInputDialog(
          "Do you want to start the calculator? (Y/N): "); 

while(endProgram != "Y" || endProgram != "y"); { 
     numEntered = Integer.parseInt(JOptionPane.showInputDialog(
            "Please enter your first number.")); 
     while(numEntered != 0); { 
      System.out.println("You entered the number " + numEntered); 
           sumTotal = sumTotal + numEntered; 
           loopCounter++; 
          numEntered = Integer.parseInt(JOptionPane.showInputDialog(
                     "Please enter the next number")); 
          break;} 


     System.out.println("The total numbers entered is: " + loopCounter + '\n' + "The total of the numbers entered is: " + sumTotal + '\n' + "The average of the numbers entered is: " + averageNum); 

     endProgram = JOptionPane.showInputDialog(
               "Would you like to do a new set of calculations? (Y/N): "); 
     break; 
} 

System.out.println("Thank you for using the calculator. The program will now end."); 
        } 
} 

takmak, ancak Şubat ayı ortasından itibaren, bu nedenle herhangi bir özensiz mazur için sadece Java ile çalışıyoruz Aşağıda kodlama veya yanlış kod kullanımı.

Eğitimsiz gözlerimde, break ifadeleri döngüler içinde görünüyor; Derleyicimin neden önerilmediğine dair kafam karıştı. Herhangi bir yardım büyük beğeni topluyor, teşekkür ederim. Iken (...);

+0

olarak yorumlanır ederken . –

+0

'" ... bu yüzden herhangi bir özensiz biçimlendirme bahane ... "' - Bunu yazmak yerine, neden kodunuzu doğru bir şekilde biçimlendirmeyi ve sonra iyi biçimlendirilmiş kodu nasıl yayınlayacağınızı öğrenmiyorsunuz? Öğrenmek o kadar da zor değil, ve ** sizden gönüllülerin size yardım etmesini istemeniz için yapmanız gereken bir şey değil mi? –

+0

Neden burada kırılma kullanıyorsunuz? –

cevap

0

while ifadelerinizden sonra noktalı virgülleri kaldırın.

Örnek: değişim

while(endProgram != "Y" || endProgram != “y”); { 
//... 
} 

için

while(endProgram != "Y" || endProgram != “y”) { 
//... 
} 

A noktalı virgül ardından ifadesi gerçekte herhangi bir koşul olmaksızın `break` deyimi sağlıyoruz bu

while(condition) {} 
1

; Bu kötü bir uygulama: P. Mola komutlarınız aslında tüm zaman döngüleri dışında.