所以我必须在课堂上制作一个节目,我遇到了一些麻烦。 我必须调用examAverage方法,它有参数,我不知道如何。另外,在用户提示方法中,我必须在主要的循环中调用用户提示方法并要求用户输入他们的考试分数3次以获得平均值。我希望我解释得很好。我不太擅长编程。
package project5;
import java.util.*;
public class Project5 {
static final int NUM_EXAMS = 3;
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
//declare variable
double Average;
double examScore1 = 0;
double examScore2 = 0;
double examScore3 = 0;
double Average = examAverage(examScore1, examScore2, examScore3) / NUM_EXAMS;
printWelcome();
userPrompts();
display();
}
static void printWelcome() {
System.out.println("Welcome to the Exam Average Calculator");
}
public static void userPrompts() {
System.out.println("Please enter your 1st exam score.");
double examScore1;
examScore1 = console.nextDouble();
System.out.println("Please enter your 2nd exam score.");
double examScore2;
examScore2 = console.nextDouble();
System.out.println("Please enter your 3rd exam score.");
double examScore3;
examScore3 = console.nextDouble();
}
public static void display() {
double examAverage = 0;
}
public static double examAverage(double examScore1, double examScore2, double examScore3, double sum, double NUM_EXAMS) {
double Average;
sum = examScore1 + examScore2 + examScore3;
Average = (double) sum / NUM_EXAMS;
return Average;
}
public static void displayAverage(double Average) {
Object[] examAverage = null;
System.out.println("Your exam average is %.2f%", examAverage);
}
public static double examAverage(double examScore1, double examScore2, double examScore3) {
double Average;
{
return double Average
答案 0 :(得分:1)
所以你的考试平均功能都搞砸了,你想要的是考试次数和个人价值并返回平均值。 因此,您可以使用这样的函数,因为您不需要将sum作为参数。
public static double examAverage(double examScore1, double examScore2, double examScore3, double NUM_EXAMS) {
double Average;
double sum = examScore1 + examScore2 + examScore3;
Average = sum / NUM_EXAMS;
return Average;
}
所以当你调用这个函数时,你需要给它4个值
所以这就叫它
double Average = examAverage(examScore1, examScore2, examScore3, 3);
这应该可以解决您的功能问题。如果我没有清楚地解释清楚,或者你是否希望我详细说明,请告诉我。
您在调用代码的顺序上也存在问题。 首先,您想要欢迎用户,然后您想要询问他们的值,然后您想使用这些值来计算平均值,然后您想要打印该平均值,所以请改为使用。
//declare variable
double Average;
double examScore1 = 0;
double examScore2 = 0;
double examScore3 = 0;
//Welcomes user, then prompts for exam values, calculates average using those values, and finally displays the average
printWelcome();
userPrompts();
double Average = examAverage(examScore1, examScore2, examScore3, 3);
System.out.println("your average is" + Average)
另外,删除你的Display()和DisplayAverage()函数,它们是没用的。
答案 1 :(得分:1)
我不想为你做功课,但你在代码中用paramsters多次调用方法。例如,在你的主要内部:
examAverage(examScore1,examScore2,examScore3)
你调用examAverage方法传入3个变量。你的三个变量都被设置为0,所以没有任何意义。你可能应该在调用examAverageMethod来初始化考试前调用你的userPrompts。
你的程序看起来差不多完成了,想想你想要怎么做的顺序。祝你好运
答案 2 :(得分:0)
在途中,通过在类级别定义来调用变量static:
<%= form_for @employer, url: bookings_path, method: :post, html: { class: "main-form", id: "create-booking" } do |f| -%>
<% @employer.errors.full_messages.each do |msg| %>
<p><%= msg %></p>
<% end %>
<div id="bookings">
<ol class="numbers">
<li>
<legend>Location, Pay, & Vehicle</legend>
<div class="form-group">
<div class="row">
<div class="col-sm-6">
<label>Type of job</label><br>
<%= f.select(:job_type_id, options_from_collection_for_select(JobType.all, :id, :name_with_delivery), {}, { id: 'job-type', class: 'form-control' }) %>
</div>
<div class="col-sm-6">
<label>Vehicle needed</label><br>
<%= f.select(:vehicle_id, options_from_collection_for_select(Vehicle.all, :id, :name), {}, { id: 'vehicle-type', class: 'form-control' }) %>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-6">
<label>Location</label>
<% if current_employer.locations.size > 1 %>
<%= f.select :location_id, options_from_collection_for_select(current_employer.locations.all, :id, :name_or_address_1), {}, { class: 'form-control' } %>
<% elsif current_employer.locations.size == 1 %>
<p><strong>Location: </strong><%#= current_employer.locations.first.name_or_address_1 %></p>
<%= f.hidden_field :location_id, current_employer.locations.first.id %>
<% end %>
<%= link_to "or add new location", new_employer_location_path(current_employer, Location.new) %>
</div>
<div class="col-sm-6">
<%= f.label :pay %>
<span id="length-message" class="pull-right" style="color: #a94442"></span>
<br>
<%= f.text_field :pay, class: 'form-control', id: 'pay', maxlength: '18' %>
</div>
</div>
</div>
</li>
<legend>Shifts</legend>
<%= render 'booking', booking: Booking.new %>
</ol>
</div>
<%= link_to "Add another shift", "javascript:;", class: 'add-shift', style: 'margin-left: 40px;' %>
<script type="text/javascript">
$(".add-shift").click(function(){
$("ol.numbers").append("<%= j render 'booking', booking: Booking.new %>")
});
</script>
<%= f.submit "Post Shifts", class: 'btn green-button pull-right' %>
<br>
<br>
<br>
<span class="error-message bg-danger pull-right">
</span>
<% end %>
其他最好的方法是将所有这些变量包装在一个类中(或者更好地创建一个列表/数组)并在userPrompts方法中传递该调用并填充它,然后使用相同的对象来计算平均值。
关于循环,请按照以下方法进行:
private static double examScore1 = 0;
private static double examScore2 = 0;
private static double examScore3 = 0;