2017-01-22 16 views
0

2つの異なる日の1日に、&日2の10の異なる都市(例えば都市1 ...都市10)の気温を格納するプログラムを構築しています。 しかし、私のプログラムは、最初の入力を正しく取り込み、switch文の最初の条件を読み込まず、それが繰り返されるたびにそれをスキップします。ループで入れ子になったループのためにループが繰り返されます

メイン、一時、検索の3つのクラスを作成しました。 tempは2d配列の値を格納するために使用され、検索は都市の名前を取得するために使用され、a)最高温度b)最低温度。

package battlefield; 

public class Main 
{ 
public static void main(String []args) 
{ 
    temp t=new temp(); 
    t.takein(); 
    search s =new search(); 
    s.sch(); 
} 
} 

package battlefield; 

import java.util.Scanner; 

public class temp 
{ 
int a[][]=new int[2][10]; 
String ch; 
Scanner sc=new Scanner(System.in); 
public temp() 
{ 
System.out.println("Default temperature have been set to 15 degree Celsius. "); 
    for(int i=0;i<2;i++) 
    { 
     for(int j=0;j<10;j++) 
     { 
      a[i][j]=15; 
     } 
    } 
    } 
    public void takein() 
    { 

    for(int i=0;i<2;i++) 
    { 
     for(int j=0;j<10;j++) 
     { 
      System.out.println("Do you want to enter more?"); 
      ch=sc.nextLine(); 
      sc.nextLine(); 
      System.out.println("Value of i="+i+" Value of j="+j); 
      switch(ch) 
      { 
       case "y": 
        {System.out.println("Enter temprature on day "+i+" city "+j); 
        a[i][j]=sc.nextInt(); 
        break;} 
       case " n": 
        continue; 
       case " e": 
        break; 
      } 
      if (ch.equals("e")) 
      { 
       break; 
      } 
     } 
     if (ch.equals("e")) 
     { 
      break; 
     } 
    } 


    } 

    } 

    package battlefield; 

    public class search extends temp 
{ 
    String rep; 
    int t=0; 
    public void sch() 
{ 
    System.out.println("Do you want to search by highest temperature or lowest?"); 
    rep=sc.nextLine(); 
    switch(rep) 
    { 
      case "h":{for(int i=0;i<2;i++) 
      { 
       for(int j=0;j<10;j++) 
       { 
        if(t<a[i][j]) 
        { 
         t=a[i][j]; 
        } 
        else 
         continue; 
       } 
      } 
      for(int i=0;i<2;i++) 
      { 
       for(int j=0;j<10;j++) 
       { 
        if(t==a[i][j]) 
        { 
         System.out.println("City "+j+" has the highest temprature of all on day "+i); 
        } 
        else 
         continue; 
       } 
      } 

      break;} 
      case "l":{for(int i=0;i<2;i++) 
      { 
       for(int j=0;j<10;j++) 
       { 
        if(t>a[i][j]) 
        { 
         t=a[i][j]; 
        } 
        else 
         continue; 
       } 
      } 
      for(int i=0;i<2;i++) 
      { 
       for(int j=0;j<10;j++) 
       { 
        if(t==a[i][j]) 
        { 
         System.out.println("City "+j+" has the lowest temperature of all on day "+i); 
        } 
        else 
         continue; 
       } 
      } 
      break;} 
      case "n":break; 
    } 
    } 
    } 

答えて

2

末尾の改行が残るのはnextInt()です。

ch=sc.nextLine(); 
sc.nextLine(); 

ch=sc.nextLine(); 
// sc.nextLine(); 

を変更したり、移動しているsc.nextLine()

case "y": 
{ 
    System.out.println("Enter temprature on day "+i+" city "+j); 
    a[i][j]=sc.nextInt(); 
    sc.nextLine(); // <-- here. 
    break; 
} 

関連する問題