简体   繁体   English

如何在另一个私有方法中使用一个方法的return语句?

[英]How do i use the return statement of one method in another private method?

This is my Code that I have so far: 到目前为止,这是我的代码:

import java.util.*;

public class VoteRecorder 
{
// Variables and instance variables
 public static String nameCandidatePresident1;
 public static String nameCandidatePresident2;
 public static String nameCandidateVicePresident1;
 public static String nameCandidateVicePresident2;
 public static int votesCandidatePresident1;
 public static int votesCandidatePresident2;
 public static int votesCandidateVicePresident1;
 public static int votesCandidateVicePresident2;
 private int myVoteForPresident;
 private int myVoteForVicePresident;


 public VoteRecorder()
 {
     nameCandidatePresident1 = "null";
     nameCandidatePresident2 = "null";
     nameCandidateVicePresident1 = "null";
     nameCandidateVicePresident2 = "null";
     votesCandidatePresident1 = 0;
     votesCandidatePresident2 = 0;
     votesCandidateVicePresident1 = 0;
     votesCandidateVicePresident2 = 0;
     myVoteForPresident = 0;
     myVoteForVicePresident = 0;     
 }


 public void setCandidatesPresident(String name1, String name2)
 {
     nameCandidatePresident1 = name1;
     nameCandidatePresident2 = name2;

 }

 public void setCandidatesVicePresident(String name1, String name2)
 {
    nameCandidateVicePresident1 = name1;
    nameCandidateVicePresident2 = name2;
 }

 public static void resetVotes()
 {
     votesCandidatePresident1 = 0;
     votesCandidatePresident2 = 0;
     votesCandidateVicePresident1 = 0;
     votesCandidateVicePresident2 = 0;   
 }

 public static String getCurrentVotePresident()
 { 
     return nameCandidatePresident1 + ":" + votesCandidatePresident1 + "\n" +
            nameCandidatePresident2 + ":" + votesCandidatePresident2;
 }

 public static String getCurrentVoteVicePresident()
 {
     return nameCandidateVicePresident1 + ":" + votesCandidateVicePresident1 +  "\n" +
            nameCandidateVicePresident2 + ":" + votesCandidateVicePresident2;
 }


 public void getAndConfirmVotes()
 {

 }


private String getVotes()
{
     Scanner keyboard = new Scanner(System.in);
     System.out.println("please vote for a President or vice president "     + nameCandidatePresident1 + ", " + nameCandidatePresident2 + ", " + nameCandidateVicePresident1
             + " or " + nameCandidateVicePresident2);
     String presidentVote = keyboard.nextLine();
     if (presidentVote.equalsIgnoreCase(nameCandidatePresident1))    
         return nameCandidatePresident1;

     if(presidentVote.equalsIgnoreCase(nameCandidatePresident2))     
         return nameCandidatePresident1;     

     System.out.println("please vote for a Vice president " + nameCandidateVicePresident1 + " or" + nameCandidateVicePresident2);
     String vicePresidentVote = keyboard.nextLine();

     if(vicePresidentVote.equalsIgnoreCase(nameCandidateVicePresident1))
         return nameCandidateVicePresident1;

     if(vicePresidentVote.equalsIgnoreCase(nameCandidateVicePresident2))
         return nameCandidateVicePresident2;

     else 
         return "not a valid vote";

}


private boolean confirmVotes()
{
   System.out.println("Your vote for President is:");
   System.out.println("your vote for Vice President is:");
   System.out.println("Is this correct? Yes or No?");
   Scanner keyboard = new Scanner(System.in);
   String answer = keyboard.nextLine();
   if (answer.equalsIgnoreCase("Yes"))
        return true;
   else 
       return false;
}

private void recordVote()
{
puscode: If confirmVotes returns true, take the nameCandidate, and ++ to votesCandidate of the   same type
Copy this If statement four times, one for each of the candidates, 2 president and 2 vp.
Else or If confirmvotes returns false, put output saying that  the votes were not confirmed.

}


}

Say i had all this code, lets look at the method getVotes() and confrimVotes(), in getVotes() the user picks a candidate and than that candidate is returned. 说我拥有所有这些代码,让我们看一下方法getVotes()和confrimVotes(),在getVotes()中,用户选择一个候选者,然后返回该候选者。 How would i get that return statement to show up else where in other methods? 我将如何获得该return语句以其他方式显示在其他位置? like in confirmVote() i want to do this 像在confirmVote()中,我想这样做

 System.out.println("Your vote for President is: (PresidentialCandidate return statement");

But how can i do that? 但是我该怎么办呢?

This is not a direct answer to your question, but I think your code could be made a lot simpler by harnessing some of the power of object-oriented programming. 这不是您问题的直接答案,但我认为,利用一些面向对象编程的功能可以使您的代码更简单。

You are storing multiple types of information about 4 candidates for different positions as separate variables, and it's making your class very unwieldy. 您正在将关于不同职位的4个候选者的多种信息存储为单独的变量,这使您的课程非常笨拙。

A (in my opinion) better approach would be to have eg a Candidate class to store information about a single candidate, and then your classes could look as follows: (在我看来)更好的方法是,例如有一个Candidate类来存储有关单个候选人的信息,然后您的类可能如下所示:

class Candidate {
    String Name;
    int votes;
}

class VoteRecorder {
     Candidate[] presidents;
     Candidate[] vicePresidents;

     Candidate myVoteForPresident;     //Or even make these both ints.
     Candidate myVoteForVicePresident;
}

The classes can be further refined, but this will be a start. 这些类可以进一步完善,但这只是一个开始。 Any time you see multiple pieces of information that describe the same entity being repeated multiple times, it's a good indication that you could simplify your life by adding a class to represent them together instead. 每当您看到描述同一实体的多次信息被重复多次时,就很好地表明您可以通过添加一个类来代替它们在一起来简化生活。

Edit (to answer question specifically): 编辑(专门回答问题):

If you want to do effectively the following: 如果要有效执行以下操作:

System.out.println("Your vote for President is: (PresidentialCandidate return statement"); System.out.println(“您对总统的投票是:(PresidentialCandidate返回声明”);

You can write something like this: 您可以这样写:

String voteResult = getVotes();
System.out.println("Your vote for President is: " + voteResult);

Or in one line: 或一行:

 System.out.println("Your vote for President is: " + getVotes());

Each time you run this code though, it will prompt the user for input. 但是,每次运行此代码时,它将提示用户输入。 If you want to save the result until next time as well, you will have to save it to an attribute, eg by having a string in your class, and assigning the value to it first. 如果您也要保存结果到下一次,则必须将其保存到属性,例如,在类中包含一个字符串,然后首先为其分配值。 Then just use that later instead of the local variable voteResult . 然后,请稍后使用它,而不是使用局部变量voteResult

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

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