简体   繁体   English

做{}while(); 似乎没有按预期工作

[英]do{}while(); seems not working as expected

Hey guys i am a recent mover from c so i don't have a deep knowledge about java.嘿伙计们,我最近从 c 搬家,所以我对 Java 没有深入的了解。 My problem is that when i run the console it seems do while is not working as the condition i tell it to but working opposite of the condition.for additional information i use eclipse as my IDE.我的问题是,当我运行控制台时,它似乎没有按照我告诉它的条件工作,但与条件相反。有关其他信息,我使用 eclipse 作为我的 IDE。

full code:完整代码:

public class kahfi {

static int choice;
static String judul = null ;
static String judulterbit;
static String penulis;
static String jenisbuku;
static String genre;


static int halamannovel ;
static int halamancerpen ;
static int harganovel;
static int hargacerpen;
static int pendapatannovel;
static int pendapatancerpen;


public static void pilihan1(){

    Scanner scanmenu2 = new Scanner(System.in);

    System.out.println("Judul:");
    judul = scanmenu2.next();

    System.out.println("Penulis:[nama mohon disambungkan jika panjang dan terpisah]");
    penulis = scanmenu2.next();

    System.out.println("novel/cerpen? ");
    jenisbuku = scanmenu2.next();

    if(jenisbuku.equals("novel")){ 



     do{
        System.out.println( "jumlah halaman novel[100-300]  :" );
         halamannovel = scanmenu2.nextInt();

     }while(halamannovel >=  100 && halamannovel <= 300 );

    System.out.println("Pilih genre [fiksi,nonfiksi,humor ,dll]: ");
    genre = scanmenu2.next(); 
    System.out.println("data telah tersimpan");
    System.out.println("=========================");

  }else if(jenisbuku.equals("cerpen")){


     do{
        System.out.println("jumlah halaman cerpen[5-30]  :");  
         halamancerpen = scanmenu2.nextInt();

     }while(halamancerpen >= 5 && halamancerpen <= 30 );

    System.out.println("Pilih genre [fiksi,nonfiksi,humor ,dll]: ");
    genre = scanmenu2.next(); 
    System.out.println("data telah tersimpan");
    System.out.println("=========================");


   }else{
       System.out.println("=========================");
      System.out.println("salah input ");
     System.out.println("=========================");
   }

}

public static void pilihan2(){

    System.out.println("Buku yang belum terbit :");
    System.out.println(judul);
    System.out.println("=========================");

    harganovel = halamannovel * 500;
    hargacerpen = halamancerpen * 200;

    System.out.println("harga novel "+(harganovel));
    System.out.println("harga cerpen "+(hargacerpen));
    System.out.println("=========================");

}

public static void pilihan3(){

    judulterbit = new String(judul);

    System.out.println("=========================");
    System.out.println("uang yang di dapatkan : ");
    Random randomno = new Random();

    pendapatannovel = randomno.nextInt(100000) * halamannovel;
    pendapatancerpen = randomno.nextInt(100000) * halamancerpen;

    System.out.println("pendapatan novel : +" + pendapatannovel);
    System.out.println("pendapatan cerpen : +" + pendapatancerpen);
    System.out.println("=========================");

}

public static void pilihan4(){
    System.out.println("=========================");
    System.out.println("buku yang sudah terbit :\n"+(judulterbit));
    System.out.println("=========================");
}


public static void main(String[] args) {


    do{
    System.out.println("Menu\n====");
    System.out.println("1.Menulis buku \n2.Melihat daftar buku yang belum terbit\n3.Menerbitkan buku\n4.Melihat daftar buku yang sudah diterbitkan\n5.Exit.");
    System.out.println("pilihan anda : ");
    Scanner scanmenu = new Scanner(System.in);
    choice = scanmenu.nextInt();

    switch(choice) {


    case 1 :

        pilihan1();

       break;

    case 2 :

        pilihan2();

        break;

    case 3 :

        pilihan3();

       break;
    case 4 :

        pilihan4();

       break;
    case 5  :
        System.exit(0);
        break;

        }
    }while(choice!=5);

    }   

}

the problem seems to be in this side .问题似乎在这一边。 Iam pretty sure the condition are right ,but when i input "85" in halamannovel it continues instead of repeating and when i input 100 it repeats .我很确定条件是正确的,但是当我在 halamannovel 中输入“85”时,它会继续而不是重复,当我输入 100 时,它会重复。 I tried to change the condition but it made it worse.It also applies to halamancerpen.我试图改变病情,但它使情况变得更糟。它也适用于halamancerpen。

 do{
        System.out.println( "jumlah halaman novel[100-300]  :" );
         halamannovel = scanmenu2.nextInt();

     }while(halamannovel >=  100 && halamannovel <= 300 );

    System.out.println("Pilih genre [fiksi,nonfiksi,humor ,dll]: ");
    genre = scanmenu2.next(); 
    System.out.println("data telah tersimpan");
    System.out.println("=========================");

  }else if(jenisbuku.equals("cerpen")){


     do{
        System.out.println("jumlah halaman cerpen[5-30]  :");  
         halamancerpen = scanmenu2.nextInt();

     }while(halamancerpen >= 5 && halamancerpen <= 30 );

Any suggestion or solution would be a life saver.任何建议或解决方案都将是救命稻草。 Thank you for your time!感谢您的时间!

FYI i tried in 3 different eclipse .仅供参考,我尝试了 3 种不同的日食。

You are using 2 Scanner objects on the same System.in stream.您在同一个 System.in 流上使用 2 个 Scanner 对象。 Instead, you should reuse the existing scanner otherwise they will interfere with each other.相反,您应该重新使用现有的扫描仪,否则它们会相互干扰。 See for instance the following: How to use multiple Scanner objects on System.in?例如,请参见以下内容: 如何在 System.in 上使用多个 Scanner 对象?

Its expected to continue when you enter 85.当您输入 85 时,它预计会继续。

In your exit criteria, you have given:在您的退出标准中,您给出了:

while(halamannovel >=  100 && halamannovel <= 300 );

This means that any number within 100(100 included) and 300(300 included) will make your loop repeat.这意味着 100(包括 100)和 300(包括 300)内的任何数字都会使您的循环重复。 The number range is 100-300.数字范围是 100-300。

If you enter 85, it is lesser than 100, and thus the while returns false and the loop breaks.如果输入 85,则它小于 100,因此while返回false并且循环中断。

Hope it helps!希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM