所以我遇到了一个问题。我正在设计一个UPC程序,它返回关于产品类型/制造商(等)的条形码数量的几个部分,并使用方程式来检查最后一个数字。这是我的问题:
我有一个处理输入和输出的Driver类,以及一个存储我所有方法的UPC类。我刚刚想出了调用方法和toString是如何工作的,我真的已经退出了它。但现在我的问题是根据分配要求输入用户的UPC的方式,我必须将数据作为部分字符串收集,然后使用parseInt运行一段代码将字符串分成多个int数字,这样我就可以运行它们了通过等式。问题是,我在驱动程序类中做了这个,并且我不确定如何在没有屠宰我的代码的情况下继续进行。
我已经尝试将使用parseInt的部分放在UPC类中,但它似乎无法正常工作。有什么方法可以将分离的变量从驱动程序类转移到UPC类并将它们输入到我将要制作的等式中吗?
这是Driver类: import java.util.Scanner;
public class Driver
{
public static void main (String[] args)
{
// Create a Scanner object
Scanner keyboard = new Scanner(System.in);
//------------------------------Variables----------------------------------------------
// Declare variables for UPC code (Digits 1-12)
int d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12;
// Declare character variables for string to individual digit conversion (d2-d11)
char c2, c3, c4, c5, c6, c7, c8, c9, c10, c11;
// User's entered UPC (Input is d1, manufacturer, product and d12)
UPC userUPC;
//-------------------------------User Input-------------------------------------------
// Prompt and get user input for all 12 digits of UPC code
System.out.println("Enter all twelve digits of your UPC code.");
System.out.println("Please enter in this format: * ***** ***** *.");
d1 = keyboard.nextInt();
String manuString = keyboard.next();
String prodString = keyboard.next();
d12 = keyboard.nextInt();
//-------------------------------String to Int conversion-----------------------------
// Convert user input strings to digits (Int)
int manuInt = Integer.parseInt(manuString);
int prodInt = Integer.parseInt(prodString);
c2 = manuString.charAt(0);
d2 = Character.getNumericValue(c2);
c3 = manuString.charAt(1);
d3 = Character.getNumericValue(c3);
c4 = manuString.charAt(2);
d4 = Character.getNumericValue(c4);
c5 = manuString.charAt(3);
d5 = Character.getNumericValue(c5);
c6 = manuString.charAt(4);
d6 = Character.getNumericValue(c6);
c7 = prodString.charAt(0);
d7 = Character.getNumericValue(c7);
c8 = prodString.charAt(1);
d8 = Character.getNumericValue(c8);
c9 = prodString.charAt(2);
d9 = Character.getNumericValue(c9);
c10 = prodString.charAt(3);
d10 = Character.getNumericValue(c10);
c11 = prodString.charAt(4);
d11 = Character.getNumericValue(c11);
//-----------------------------------UPC----------------------------------------------
// Create user UPC
userUPC = new UPC(d1, manuInt, prodInt, d12);
resultUPC = new UPC
//----------------------------Program Output------------------------------------------
// Output data
System.out.println("The item's UPC value is: " + userUPC.toString());
System.out.println();
System.out.println("Product Type: " + userUPC.getItemType());
System.out.println("Manufacturer: " + userUPC.getManufacturer());
System.out.println("Product: " + userUPC.getProduct());
System.out.println();
System.out.println("Calculated Check Digit: " /*+ userUPC.getCalculatedCheck()*/);
System.out.println("UPC Check Digit: " + userUPC.getCheckDigit());
}
}
这是我的UPC课程:
public class UPC
{
// Instance variables
private int itemType; // digit 1
private int manufacturer; // digits 2,3,4,5,6
private int product; // digits 7,8,9,10,11
private int checkDigit; // digit 12
// Constructer method: Initialize instance variables
public UPC(int item, int man, int prod, int check)
{
itemType = item;
manufacturer = man;
product = prod;
checkDigit = check;
}
// Return the UPC code with "-" inserted between the different secions of the barcode
public String toString()
{
return itemType + "-" + manufacturer + "-" + product + "-" + checkDigit;
}
// Return the UPC's item type (First digit)
public int getItemType()
{
return itemType;
}
// Return the manufacturer code (Digits 2-6)
public int getManufacturer()
{
manufacturer = man
return manufacturer;
}
// Return the product code (Digits 7-11)
public int getProduct()
{
return product;
}
// Return the check digit (Digit 12)
public int getCheckDigit()
{
return checkDigit;
}
/* Calculate and return the calculated check digit (Equation)
public int getCalculatedCheckDigit()
{
} */
}
我正在测试的代码中可能有一些无用的代码,但除此之外一切都有效。在此先感谢,我很抱歉文字的墙壁。 :)
答案 0 :(得分:0)
我认为你应该在UPC类中有2个构造函数。一个接受字符串,另一个接受(int item,int man,int prod,int check);
接受字符串的构造函数将使用扫描程序从中获取数字并使用它们调用其他构造函数。这样一切都将封装在UPC类中。
import java.util.Scanner;
public class Driver {
public static void main(String[] args) {
// Create a new scanner to help you get information from the input
// stream.
Scanner scanner = new Scanner(System.in);
// Prompt and get user input for all 12 digits of UPC code
System.out.println("Enter all twelve digits of your UPC code.");
System.out.println("Please enter in this format: * ***** ***** *.");
// get the next input
String input = scanner.nextLine();
Upc userUpc = new Upc(input);
// Output data
System.out.println("The item's UPC value is: " + userUpc.toString());
System.out.println();
System.out.println("Product Type: " + userUpc.getItemType());
System.out.println("Manufacturer: " + userUpc.getManufacturer());
System.out.println("Product: " + userUpc.getProduct());
System.out.println();
System.out.println("Calculated Check Digit: " //+ userUPC.getCalculatedCheck()
);
System.out.println("UPC Check Digit: " + userUpc.getCheckDigit());
scanner.close();
}
}
class Upc {
// Instance variables
private int itemType; // digit 1
private int manufacturer; // digits 2,3,4,5,6
private int product; // digits 7,8,9,10,11
private int checkDigit; // digit 12
// constructor that accepts String
public Upc(String upcString) {
//Scanner for the given input that will help us get data from it
Scanner scanner = new Scanner(upcString);
itemType = scanner.nextInt(); //get item type
manufacturer = scanner.nextInt(); //get manufactor
product = scanner.nextInt(); //get product
checkDigit = scanner.nextInt(); //get chackDigit
setVariables(itemType, manufacturer, product, checkDigit);
scanner.close();
}
public Upc(int item, int man, int prod, int check)
{
setVariables(item, man, prod, check);
}
private void setVariables(int item, int man, int prod, int check)
{
this.itemType = item;
this.manufacturer = man;
this.product = prod;
this.checkDigit = check;
}
// Return the UPC code with "-" inserted between the different secions of
// the barcode
public String toString() {
return itemType + "-" + manufacturer + "-" + product + "-" + checkDigit;
}
// Return the UPC's item type (First digit)
public int getItemType() {
return itemType;
}
// Return the manufacturer code (Digits 2-6)
public int getManufacturer() {
return manufacturer;
}
// Return the product code (Digits 7-11)
public int getProduct() {
return product;
}
// Return the check digit (Digit 12)
public int getCheckDigit() {
return checkDigit;
}
// Calculate and return the calculated check digit (Equation)
// public int getCalculatedCheckDigit() {
//
// }
}