有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在Java中使用多个类创建程序时遇到的类问题

我正在尝试创建一个程序,其中用户输入四位数字,并给出一个六位数字。前四位是用户输入的数字,第五位是((digit1+digit2+digit4)%10),第六位是第三位。这是我到目前为止所拥有的。已提供用于用户输入的类。感谢您的帮助

import java.lang.Math;

/**
* Purpose: This class/program generates a 6 digit card number based on 4 digits inputed by the user
*/
public class CardGenerator
{
   public void main(String[] args)
   {    
      UserInteraction input = new UserInteraction();
      calculateDigit5 digit5Calc = new calculateDigit5();
      getDigit digit = new getDigit();
      getRemainder remainder = new getRemainder();

      int number = input.getIntValueFromUser("Enter the four digit card number"),
          digit1 = getDigit(number, 3),
          number = getRemainder(number, 3),
          digit2 = getDigit(number, 2),
          number = getRemainder(number, 2),
          digit3 = getDigit(number, 1),
          number = getRemainder(number, 1),
          digit4 = getDigit(number, 0),
          digit5 = calculateDigit5,
          digit6 = digit3;

      System.out.println("The 6 digit card number is " + digit1 + digit2 + digit3 + digit4 + digit5
      + digit6);
   }

   public class getDigit
   {
     int digitWeight = Math.pow(number, (1/1000)),
                 rem = getRemainder(number, digitPosition),
               digit = (originalNumber - rem) / digitWeight;


   }

   public class getRamainder
   {

   }

   public class calculateDigit5
   {
     int digit5 = (digit1 + digit2 + digit4) % 10;
   }
}

这是提供给我的

import java.util.*;
//DO NOT PUT OTHER METHODS IN THIS CLASS or make any changes to it
public class UserInteraction
{
    public String getStringValueFromUser(String message)
    {
        String value = "";
        Scanner input = new Scanner(System.in);
        System.out.print(message + " : ");
        value = input.nextLine();
        return value;
    }
//DO NOT PUT OTHER METHODS IN THIS CLASS or make any changes to it
    public double getDoubleValueFromUser(String message)
    {
        double value = 0;
        Scanner input = new Scanner(System.in);
        System.out.print(message + " : ");
        value = input.nextDouble();
        return value;
    }
//DO NOT PUT OTHER METHODS IN THIS CLASS or make any changes to it
    public int getIntValueFromUser(String message)
    {
        int value = 0;
        Scanner input = new Scanner(System.in);
        System.out.print(message + " : ");
        value = input.nextInt();
        return value;
    }

  //DO NOT PUT OTHER METHODS IN THIS CLASS or make any changes to it
}

共 (1) 个答案

  1. # 1 楼答案

    public class AllDigits {
    
    private int[] digits;
    
    public AllDigits()
    {
        digits = new int[6];
    }
    
    public int getDigit(int index)
    {
        return digits[index];
    }
    
    public void setDigit(int index, int value)
    {
        if(index >= 0 && index <=3)
        {
            digits[index] = value;
            //if we are setting the 3rd number set the 6th as well
            if(index == 2)
                digits[5] = value;
        }
        //this is for the fifth digit
        else if(index == 4)
        {
            digits[index] = (digits[0] + digits[1] + digits[3]) % 10;
        }
    }
    
    public void printDigit()
    {
        for(int i = 0; i < digits.length; i++)
        {
            System.out.print(digits[i]);
            if(i != digits.length - 1)
                System.out.print(" ");
        }
        System.out.println();
    }
    }
    

    这应该足以让你开始。有无数种方法可以做到这一点,但这是我的实现