Click here to Skip to main content
14,697,418 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is a simple problem for the calculation of gain percentage.

There is a test case which says my code doesn't check for repair amount.

What I have tried:

import java.util.Scanner;
class Gain {
   public static void main(String[] args){
      double g=0;
      int gain;
      Scanner in=new Scanner (System.in);
      System.out.println("Price of old scooter:");
      int p=in.nextInt()
      System.out.println("The amount spent for    repair:");
      int r=in.nextInt();
      System.out.println("Sold Price:");
      int s=in.nextInt();
      int cost=p+r;
      gain=s-cost;
      if((p>e)&&(r>0)&&(s>0)){
         if(gain>0){
            g=((gain/cost) *100);
            int r=in.nextInt();
            System.out.println("Sold Price:");
            int s=in.nextInt();
            int cost=p+r;
            gain=s-cost;
            if((p>0)&&(r>0)&&(s>0)) {
               if(gain>0){
                  g=((gain/cost)*100);
                  System.out.printf("Gain percentage is %.2f",g);
               }
               else{
                  System.out.println("Unable to calculate Gain Percentage");
               }
            }
            else{
               System.out.println("Incorrect Inputs");
            }
         }
      }
Posted
Updated 2-Feb-20 10:42am
v2
Comments
Richard MacCutchan 3-Feb-20 4:52am
   
The above code will not even compile, as there are syntax errors (missing semi-colons), and duplicate variable declarations. You need to fix those problems first. Your code is also very difficult to understand with those single letter variable names. Use proper meaningful names and you are more likely to see where it goes wrong.

1 solution

We have no idea what your code is supposed to do, let alone what your test data looks like, or what answer you were expected compared to what you did get. Without that and your code running against the data, there isn't really anything we can do tom fix it for you.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
   
Comments
Member 14733155 3-Feb-20 1:16am
   
The sample input and output is as follows:
input:
Price of old scooter:
4700
The amount spent on repair:
Sold Price:
5800
output :
Gain Percentage:5.45
OriginalGriff 3-Feb-20 1:28am
   
And? What does the debugger show you is going on?
Member 14733155 3-Feb-20 14:39pm
   
im writing this on an online ide, dont know how to debug
OriginalGriff 3-Feb-20 15:37pm
   
At the risk of repeating myself: "... a quick Google for the name of your IDE and "debugger" should give you the info you need."

If you don't learn to use it on little apps like this, how do you expect to cope when you get to normal sized ones: 20,000 lines or 100,000 lines of code?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900