I am having some trouble getting my sum function to work. I am trying to create a program that asks for a product number and a quantity using switch structure and a sentinel loop. It will run until 0 is entered. It should calculate the total number of products entered and the total value of all products entered. The quantity works just fine. It's the total value that will only work for the 1st product entered. I cannot get the sum for total to keep adding until 0 is pressed. Any help would be greatly appreciated!!
package com.jpvander.githubjobs.activities.searches.saved;
import android.app.ActionBar;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import com.jpvander.githubjobs.R;
import com.jpvander.githubjobs.datasets.GitHubJob;
import java.util.HashMap;
import java.util.LinkedHashMap;
public class ViewJobDetailsFragment extends Fragment {
private static final float PADDING_DP = 10.0f;
private OnFragmentInteractionListener mListener;
private GitHubJob jobSelected;
public ViewJobDetailsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View jobDetailsView = inflater.inflate(R.layout.fragment_view_job_details, container, false);
TableLayout jobDetailsTable = (TableLayout) jobDetailsView.findViewById(R.id.table_layout);
if (null == jobSelected) {
jobSelected = new GitHubJob();
jobSelected.setTitle("Product Tester");
jobSelected.setCompany("ACME");
jobSelected.setLocation("Monument Valley, UT");
jobSelected.setDescription("Looking for a bright but accident-prone coyote for product testing");
}
LinkedHashMap<String, String> rowData = new LinkedHashMap<>();
rowData.put("Company:", jobSelected.getCompany());
rowData.put("Location:", jobSelected.getLocation());
rowData.put("Title:", jobSelected.getTitle());
rowData.put("Description:", jobSelected.getDescription());
TableRow tableRow;
TextView label;
TextView content;
float displayScale = this.getActivity().getResources().getDisplayMetrics().density;
int paddingPx = (int) (PADDING_DP * displayScale + 0.5f);
for (String key : rowData.keySet()) {
tableRow = new TableRow(this.getActivity());
tableRow.setPadding(0, paddingPx, paddingPx, paddingPx);
label = new TextView(this.getActivity());
label.setPadding(0, 0, paddingPx, 0);
label.setText(key);
content = new TextView(this.getActivity());
content.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT, 1f));
content.setText(rowData.get(key));
tableRow.addView(label);
tableRow.addView(content);
jobDetailsTable.addView(tableRow);
}
return jobDetailsView;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public void setJobSelected(GitHubJob job) {
this.jobSelected = job;
}
public interface OnFragmentInteractionListener {
void onViewJobDetailsInteraction(GitHubJob job);
}
}
答案 0 :(得分:1)
这里有几个范围问题。
首先,您已将switch语句放在循环之外。你应该将它放在循环中。
其次,sum2
存在范围问题。它在你的sentinel循环中声明,但在外面引用。我不确定为什么你有一个嵌套循环添加到sum2
。以下是解决了这些问题的代码:
public class Mailorder {
public static void main(String[] args) {
//create a scanner
Scanner input = new Scanner(System.in);
//declare variables
double product1 = 3.75;
double product2 = 5.95;
double product3 = 8.75;
double product4 = 6.92;
double product5 = 8.75;
double product6 = 7.87;
//read in product #
System.out.print("Enter a product number: ");
int product = input.nextInt();
//read in quantity sold
System.out.print("Enter quantity sold for 1 day: ");
int quantity = input.nextInt();
//keep reading data until the input is 0
int sum1 = 0;
int sum2 = 0;
while (quantity != 0) {
sum1 += quantity;
double total = 0.00;
//switch case
switch (product)
{
case 1: total += product1 * quantity; break;
case 2: total += product2 * quantity; break;
case 3: total += product3 * quantity; break;
case 4: total += product4 * quantity; break;
case 5: total += product5 * quantity; break;
case 6: total += product6 * quantity; break;
default: System.out.println("ERROR: Invalid product number");
}
sum2 += total;
//read the next data
System.out.print("Enter a product number: ");
product = input.nextInt();
System.out.print("Enter quantity sold for 1 day: ");
quantity = input.nextInt();
}
//print results
System.out.println("The total number of products sold last week " + sum1);
System.out.println("The total retail value of all products sold last week " + sum2);
}
}