从main方法调用方法?

时间:2017-03-30 21:58:40

标签: java

我试图制作2种方法。 main方法有用户输入,另一个叫ShowNum的方法应该输出...输出。以下是一个主要方法的原始代码:

public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
int days;
int num;

System.out.print("Enter number of days: ");
days = keyboard.nextInt();   

System.out.print("Enter number for first day: ");   
num = keyboard.nextInt();

int half = days/2;
int newnum = num + 2;     

      System.out.println("Day \t Number"); 

          for (int current_day = 1; current_day <= half; current_day++){        
              newnum = newnum - 2;                                            
              System.out.println(current_day + "\t" + (newnum + 2));
          }
          for (int current_day = half + 1; current_day <= days; current_day++){ 
              newnum++;                                                        
              System.out.println(current_day + "\t" + (newnum + 2)); 
          }

我试过这个:

public static void DisplayNum(){          
   int half = days / 2; 
   int newnum = num + 2;                                              

   System.out.println("Day \t Num"); 

       for (int current_day = 1; current_day <= (days/2); current_day++){        
            newnum = newnum - 2;                                            
            System.out.println(current_day + "\t" + (num + 2)); 
              }
       for (int current_day = (days/2) + 1; current_day <= days; current_day++){ 
                  newnum++;                                                        
                  System.out.println(current_day + "\t" + (num + 2)); 
              }
}

public static void main(String[] args){

    Scanner keyboard = new Scanner(System.in);
        int days;                                          
        int num;                                      

          System.out.print("Enter number of days in the period of time: ");   
            days = keyboard.nextInt();                                        

          System.out.print("Enter number for first day: ");               
            num = keyboard.nextInt();                                        



           DisplayNum();

它是一堆乱七八糟的错误,因为变量并非都在两种方法中定义,但我不知道如何定义它们而不仅仅在一种方法中进行定义。这基本上是我教授所说的,但我很困惑。我很感激帮助或任何其他建议。提前谢谢。

3 个答案:

答案 0 :(得分:0)

将参数添加到函数DisplayNum()中,如DisplayNum(int days, int num),然后将此方法调用到main方法中 DisplayNum(days,num)

答案 1 :(得分:0)

将要使用的变量从main方法传递为DisplayNum方法的参数

  <table class="table-fill">

<tbody class="table-hover">
<tr>
<td class="text-left"><input type = "text" class = "form-control" 
               name = "name"></td>
<td class="text-left">name</td>
</tr>
<tr>
<td class="text-left"><input type = "text" class = "form-control" 
               name = "Quantity" id="QTY"></td>
<td class="text-left">QTY</td>
</tr>
<tr>
<td class="text-left"><input type = "text" class = "form-control" 
                             name = "Type" id="type"></td>
<td class="text-left">type</td>
</tr>
<tr>
<td class="text-left"><input type = "text" class = "form-control" 
               name = "price" id="prs"></td>
<td class="text-left">price</td>
</tr> 
<tr>
<td class="text-left"><input type="file" name="fileToUpload" id="fileToUpload"></td>
<td class="text-left">image</td>
</tr>
<tr>
    <td class="text-left">  <input type="submit" value="Submit1" name="Submit1"></td>
<td class="text-left">Register</td>
</tr>
</tbody>
</table>
                        

	</div>
        <div id="footer">
		<div>
			
		</div>
	</div>
         <?php
 
   $dbhost = 'localhost';
   $dbuser = 'root';
   $dbpass = '';
   
    
   $conn = mysql_connect($dbhost, $dbuser, $dbpass);
   
   if(! $conn ) {
      die('Could not connect: ' . mysql_error());
   }
  
   if (isset($_post('Submit1')))
   {
   $name=filter_input(INPUT_POST,'name');
   $QTY=filter_input(INPUT_POST,'Quantity');
   $type=filter_input(INPUT_POST,'Type');
   $sql = "INSERT INTO products (ProductName, Quantity,Price)
    VALUES ('$name','$QTY','$type')";


//('$_POST['name']','$_POST['Quantity']','$_POST['Type']')";      
   mysql_select_db('pharmacynew');
   $retval = mysql_query( $sql, $conn );
   
   if(! $retval ) {
      die('Could not enter data: ' . mysql_error());
   }
   
   echo "Entered data successfully\n";
   
   mysql_close($conn);
}       // put your code here
        ?>
    </body>
</html>

然后在main方法中传入参数值

    public static void DisplayNum(int days,int num){          
   int half = days / 2; 
   int newnum = num + 2;                                              

   System.out.println("Day \t Num"); 

       for (int current_day = 1; current_day <= (days/2); current_day++){        
            newnum = newnum - 2;                                            
            System.out.println(current_day + "\t" + (num + 2)); 
              }
       for (int current_day = (days/2) + 1; current_day <= days; current_day++){ 
                  newnum++;                                                        
                  System.out.println(current_day + "\t" + (num + 2)); 
              }
    }

答案 2 :(得分:0)

您需要将threads = [] times = ["Sat Oct 11 17:13:46 UTC 2003", "Sat Oct 11 17:13:46 UTC 2004"] for time in times: thread = threading.Thread(target=dateutil.parser.parser, args=(time)) threads.append(thread) for thread in threads: thread.start() map(lambda thread: thread.join(), threads) days值广告参数传递给temp方法。
您应该定义DisplayNum并从public static void DisplayNum(int days int temp)方法中调用DisplayNum(days, temp)