所以我遇到了一个问题。我正在设计一个UPC程序,它返回条形码中关于产品类型/制造商(等)的几个部分,并且使用一个等式来检查最后一个数字。不过,我的问题是:
我有一个处理输入和输出的驱动程序类,以及一个我所有方法都存储在其中的UPC类。我刚刚弄清楚了调用方法和toString是如何工作的,我对此感到非常兴奋。但是现在我的问题是如何根据分配需求输入用户的UPC,我必须将数据作为部分字符串收集起来,然后使用parseInt运行一段代码,将字符串分离成多个int位,这样我就可以在等式中运行它们。问题是,我是在driver类中这样做的,我不知道如何在不屠杀代码的情况下继续工作。
我尝试过将使用parseInt的部分放在UPC类中,但它似乎不能正常工作。有没有办法将分离的变量从驱动程序类带到UPC类,并将它们输入到我将要建立的方程中?
以下是驱动程序类:导入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()
{
} */
} 可能有一些无用的代码在那里我正在测试,但除此之外,所有的工作。提前谢谢,我为文字墙感到抱歉。:)
发布于 2014-02-20 09:50:29
我认为您应该在UPC类中有两个构造函数。一个接受字符串,另一个接受(int项、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() {
//
// }
}https://stackoverflow.com/questions/21903801
复制相似问题