如何将字符串作为方法参数传递

时间:2018-06-22 03:49:35

标签: java if-statement ide jgrasp

嗨,我是一名初学者Java程序员,正在创建一个Java程序,该程序将打印指令以给出给定定义批处理大小的特定字符串来烘烤cookie。我正在获取标识符,非法类型错误以及枚举预期错误。请帮忙!我太困了。我希望能够使用用户输入的烤箱尺寸来确定将哪些变量分配给每种配料。


import java.util.Scanner;

public class BakingCookies{
public static void main (String [] args) {



   int cookies;
   int ovenTemp;
   int cookTime;




         System.out.println("What is the size of your oven?\n(Your options are: Small, Medium & Large)");


          Scanner in = new Scanner(System.in);


        String oven = in.nextLine();

         System.out.println("How many cookies would you like to make?");

           int cookies = in.nextInt();




         System.out.println("To make around "+ cookies +" cookies, please do the following:");

      makeTheDough();
      cookTheDough();
      finishYourTreats();
      }



public static void makeTheDough(String oven){


  int cupsofflour ;
   int cupsofbutter;
   int teaspoonsfsugar;
   int singleeggs ;


   if (oven.equals("Small")){


   int cupsofflour = 1;
   int cupsofbutter = 1;
   int teaspoonsfsugar = 5;
   int singleeggs = 1;
   }
    else  if  (oven.equals ("Medium")){


   int cupsofflour = 2;
   int cupsofbutter = 1;
   int teaspoonsfsugar = 10;
   int singleeggs = 2;
     }
   else  if   (oven.equals ("Large")){

   int cupsofflour = 3;
   int cupsofbutter = 2;
   int teaspoonsfsugar = 15;
   int singleeggs = 3; 
    }
    }
System.out.print("Preheat the oven to the degrees stated below. Mix together:"+ cupsofflour +"cups of flour,"+ cupsofbutter+ "cups of butter," + teaspoonsofsugar + "teaspoons of sugar, and"+ singleeggs +"eggs" "Then knead the dough to make a thick, but flexible piece of dough we will now shape into cookies!");//


   }

   public static void cookTheDough (String oven){




   if (oven.equals("Small")){

   int cookies= 24;
   int ovenTemp= 330;
   int cookTime=30 ;
   }
   else if (oven.equals("Medium")){

   int cookies = 12;
   int ovenTemp = 350;
   int cookTime = 25; 
   }
   else if (oven.equals("Large")){

   int cookies = 6;
   int ovenTemp = 325;
   int cookTime = 20;
   }




   System.out.print("take your dough and use the cut tool to make at the most" cookies "cookies. Place them on a tray, and stick them in the overat" ovenTemp + " degrees. Set your timer for"+ cookTime+"minutes");

  } 

  public static void finishYourTreat(String oven){

if (oven == small){

   int coolTime = 10;
 }
System.out.print("Watch them! when the timer is up, take them out and let them cool for "  + coolTime + "minutes.");
System.out.print("Your cookies are done! Eat and enjoy the fruits of your labor!");
 }




}
  }
  }

2 个答案:

答案 0 :(得分:2)

我已解决您的问题。在System.out.println()的第70行和第104行中,由于无法识别变量,因此未使用+ variable +就使用了变量。

public class MyClass {
    public static void main (String [] args) {



   int cookies;
   int ovenTemp;
   int cookTime;




         System.out.println("What is the size of your oven?\n(Your options are: Small, Medium & Large)");


          Scanner in = new Scanner(System.in);


        String oven = in.nextLine();

         System.out.println("How many cookies would you like to make?");

           int cookies = in.nextInt();




         System.out.println("To make around "+ cookies +" cookies, please do the following:");

      makeTheDough();
      cookTheDough();
      finishYourTreats();
      }



public static void makeTheDough(String oven){


  int cupsofflour ;
   int cupsofbutter;
   int teaspoonsfsugar;
   int singleeggs ;


   if (oven.equals("Small")){


   int cupsofflour = 1;
   int cupsofbutter = 1;
   int teaspoonsfsugar = 5;
   int singleeggs = 1;
   }
    else  if  (oven.equals ("Medium")){


   int cupsofflour = 2;
   int cupsofbutter = 1;
   int teaspoonsfsugar = 10;
   int singleeggs = 2;
     }
   else  if   (oven.equals ("Large")){

   int cupsofflour = 3;
   int cupsofbutter = 2;
   int teaspoonsfsugar = 15;
   int singleeggs = 3; 
    }

System.out.println("Preheat the oven to the degrees stated below. Mix together:"+ 
cupsofflour +"cups of flour,"+ cupsofbutter+ "cups of butter," + teaspoonsofsugar +
"teaspoons of sugar, and"+ singleeggs +"eggs Then knead the dough to make a thick, but flexible piece of dough we will now shape into cookies!");


   }

   public static void cookTheDough (String oven){




   if (oven.equals("Small")){

   int cookies= 24;
   int ovenTemp= 330;
   int cookTime=30 ;
   }
   else if (oven.equals("Medium")){

   int cookies = 12;
   int ovenTemp = 350;
   int cookTime = 25; 
   }
   else if (oven.equals("Large")){

   int cookies = 6;
   int ovenTemp = 325;
   int cookTime = 20;
   }




   System.out.println("take your dough and use the cut tool to make at the most"+ cookies+ "cookies. Place them on a tray, and stick them in the overat"+ ovenTemp + " degrees. Set your timer for"+ cookTime+"minutes");

  } 

  public static void finishYourTreat(String oven){

if (oven == small){

   int coolTime = 10;
 }
System.out.println("Watch them! when the timer is up, take them out and let them cool for "  + coolTime + "minutes.");
System.out.println("Your cookies are done! Eat and enjoy the fruits of your labor!");
 }





}

答案 1 :(得分:2)

首先,一旦声明了变量,就不应将其声明回来。例如您的代码;

    <!DOCTYPE html>
    <html>
    <body>

      <p>Click the button to join two arrays.</p>
      <button onclick="myFunction()">Try it</button>
      <p id="demo"></p>
    <script>
var hege = [1, 2,4,6,7,8,8];
var stale = [1, 2,4,5];
function myFunction() {
    console.log((hege.length > stale.length))    
    var children  = (hege.length > stale.length)? abc1() :abc2();       document.getElementById("demo").innerHTML = children;
}
function abc1(){
    console.log(hege,"Abc1")    
    var abcd=hege.map(function (num, idx) {
        console.log(hege.length , idx)
        return stale.length>idx?num + stale[idx]:num;
    })
    return abcd;
}

function abc2(){

    console.log(hege,"Abc2",stale)    
    var abcd=stale.map(function (num, idx) {
        console.log(hege.length , idx)
        return hege.length>idx?num + hege[idx]:num;
    })
    return abcd;
}
</script>

</body>
</html>

此变量声明和初始化是错误的,应该这样更改;

int cookies;
int cookies = in.nextInt();

接下来,当您调用方法时,例如,也传递参数;

int cookies;
cookies = in.nextInt();

在Java中,您也不能在未初始化的情况下使用声明的变量。在您的代码中,所有方法print语句都因此而无法正常工作,并且格式不正确。(使用makeTheDough(oven); 进行字符串连接。)

还要注意您的代码流,并使用+小心地分隔块。

根据您的代码,工作代码如下。

{}