Ahoj! Delam program kde uzvatel zada cisla a pak se nakone sectou a najdou maximalni a minimalni hodnotu. Mam zde ale logickou chybu. Kdyz zadam 4 ve vsech polickach + 1 bonus bod vyjde mi 17. Ma to ale byt 25. Pomuze mi prosim nekdo najit logickou chybu?
zde je muj kod
import java.io.*;
class P17_IBO {
static BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
public static void main (String[] args) throws IOException {
//variables
String first,last,yn;
int i, slmark=0, hlmark=0, low=999, high=-999, bonus;
int sltotal=0, hltotal=0;
boolean done,rangeCheck;
done=false;
while (done==false) {
sltotal=0;
hltotal=0;
low=999;
high=-999;
System.out.println("Please enter FIRST name: ");
first=stdin.readLine ();
System.out.println("Please enter LAST name: ");
last=stdin.readLine ();
rangeCheck=false;
for (i=0;i<3;i++) {
slmark=0;
sltotal=0;
System.out.print("Please input SL subject mark " + (i+1) + ":");
slmark = validatedIntegerInput();
sltotal = sltotal + slmark;
rangeCheck=false;
while(rangeCheck==false) {
if ((slmark>0)&&(slmark<8)) {
rangeCheck=true;
sltotal = sltotal + slmark;
}
else {
System.out.print("Enter a SL mark from 1 - 7 "+ (i+1)+" :");
slmark = validatedIntegerInput();
}
}
if (slmark<low) {
low = slmark;
}
if (slmark>high) {
high = slmark;
}
}
for (i=0;i<3;i++) {
hlmark=0;
hltotal=0;
System.out.print("Please input HL subject mark " + (i+1) + ":");
hlmark = validatedIntegerInput();
hltotal = hltotal + hlmark;
rangeCheck=false;
while(rangeCheck==false) {
if ((hlmark>0)&&(hlmark<8)) {
rangeCheck=true;
hltotal = hltotal + hlmark;
}
else {
System.out.print("Enter a HL mark from 1 - 7 "+(i+1)+" :");
hlmark = validatedIntegerInput();
}
}
if (hlmark<low) {
low = hlmark;
}
if (hlmark>high) {
high = hlmark;
}
}
System.out.print("Enter bonus mark: ");
bonus = validatedIntegerInput();
bonus = bonus + sltotal + hltotal;
System.out.println(first+" "+ last);
System.out.println("Total marks is: " + bonus);
System.out.println("Lowest mark is: " + low);
System.out.println("Highest mark is: " + high);
if (bonus>=24) {
System.out.print("Diploma awarded\n");
}
else {
System.out.print("FAIL\n");
}
System.out.print("Do you want more students? (Y/N): ");
yn = stdin.readLine ();
if (yn.toUpperCase().startsWith("Y")){
done=false;
}
else {
done=true;
}
} }
public static int validatedIntegerInput() throws IOException
{
// local variables
int result=0;
boolean ok = false;
while (!ok)
{
try
{
result = Integer.parseInt(stdin.readLine());
ok = true;
}
catch (NumberFormatException error)
{
System.out.println("Please type a number");
ok = false;
}
}
return result;
}
}
Fórum › Java
Logicka chyba

Děláš tam blbosti s proměnnýma hltotal a sltotal. Jsou zbytečně inicializované třikrát a hlavně v každém cyklu při načítání. Takže každý cyklus začínáš s sltotal = 0. Akorat pri posledním načteš 4, pak provedeš rangeCheck, kde máš sltotal = sltotal + slmark, takže to bude sltotal = 4+4. To samé s hltotal. Takže nakonec máš 8+8+1 = 17. Vyndej ty total před for cyklus a přičítej až při kontrole mezí a bude ti to fungovat.

Přidej příspěvek
Toto téma je starší jak čtvrt roku – přidej svůj příspěvek jen tehdy, máš-li k tématu opravdu co říct!
Ano, opravdu chci reagovat → zobrazí formulář pro přidání příspěvku
×Vložení zdrojáku
×Vložení obrázku
×Vložení videa
Aktuálně jsou podporována videa ze serverů YouTube, Vimeo a Dailymotion.
×






























































Uživatelé prohlížející si toto vlákno
Uživatelé on-line: 0 registrovaných, 50 hostů
Podobná vlákna
8 Královen - skrytá logická chyba — založil Petrroll
Logicka odpoved na otazku — založil Ivka79
Logická tabulka - program — založil Azkry
Logická úloha-množiny — založil Nikol31
Kubikon - logická hra pro Windows 8 — založil Petr Kubíček
Moderátoři diskuze