在我的学校项目中,老师要求我们不使用任何字符串方法,但我需要隔离第一个数字和第二个数字。这个数字将从6到9位长。
小程序将产品的型号代码进行分解。拳头编号将与类别(C(相关联,该类别中的第二个具有(p(在此类别中,其余的将被打印为型号。
您知道如何实现它吗?
这是完整的程序:
public class GestionCodesProduits {
/**
*
* @param args
*/
public static void main(String[] args) {
//CONSTANTE
final String MSG_PRESENTATION = "Ce programme permet de creer des codes de produits electroniquesn"
+ "et d'afficher l'information sur un produit selon son codennn";
final String MSG_MENU_PRINCIPAL = "----nMENUn----nn1.Construire un code produitn2.Afficher les informations sur un produit"
+ "n3.QuitternnEntrez votre choix (1,2 ou 3) : ";
final String MSG_CATEGORIES = "----------nCATEGORIESn----------nnSelectionner type de produit:n"
+ "1.Ordinateursn2.Televiseursn3.Appareils photon4.Petits electrosnnn"
+ "Entrez le numero de categorie du produit : ";
final String MSG_MENU_ORDI = "--------nPRODUITS : n--------nn1.Ordinateur de bureaun2.Portablen3.Tablette"
+ "n4.Moniteurn5.Imprimante/Numerisateur"
+ "nnEntrez le numero du produit : ";
final String MSG_MENU_TELE = "--------nPRODUITS : n--------nn1.Televiseurn2.Projecteur"
+ "nnEntrez le numero du produit : ";
final String MSG_MENU_PELECTROS = "--------nPRODUITS : n--------nn1.Cafetieren2.Robot culinairen3.Melangeurn4.Batteur"
+ "n5.Friteuse/Grilln6.Grille-Pain/Four Grille-Pain"
+ "nnEntrez le numero du produit : ";
final String MSG_INFO_CATEGORIES = "CATEGORIE : ";
final String MSG_INFO_PRODUIT = "PRODUIT : ";
final String MSG_INFO_NUM_MODELE = "NUMERO DE MODELE :";
final String MSG_END_PROGRAM = "nnFIN NORMALE DU PROGRAMME";
final String MSG_PRODUCT_CODE = "ENTREZ LE CODE DU PRODUIT : ";
final String MSG_NUM_MODELE = "Entree numero de modele : ";
final String MSG_RETOUR = "Appuyez sur <ENTREE> pour revenir au menu...";
final String MSG_ERR_MENU = "nErreur ! Vous devez entrer 1, 2 (ou 3 pour quitter). nRecommencez...";
final String MSG_ERR_CATEGORIES = "nErreur ! Entrez un numero entre 1 et 4. nRecommencez...";
final String MSG_ERR_ORDI = "nErreur ! Entrez un numero entre 1 et 5. nRecommencez...";
final String MSG_ERR_TELE = "nErreur ! Entrez un numero entre 1 et 2. nRecommencez...";
final String MSG_ERR_PELECTRO = "nErreur ! Entrez un numero entre 1 et 6. nRecommencez...";
final String MSG_ERR_NUM_MODELE = "nErreur ! Le numero du modele est invalide. nRecommencez...";
final String MSG_ERR_CODE = "*** Ce code de produit est invalide ***";
//VARIABLE
char selectionMenuPrincipal;
char selectionCategories;
char selectionProduits;
int selectionNumModele;
int codeProduit;
// AFFICHAGE MENU PRESENTATION
System.out.println(MSG_PRESENTATION);
//AFFICHAGE MENU PRINCIPAL
do {
System.out.print(MSG_MENU_PRINCIPAL);
selectionMenuPrincipal = Clavier.lireCharLn();
while (selectionMenuPrincipal < '1' || selectionMenuPrincipal > '3') {
System.out.print(MSG_ERR_MENU);
selectionMenuPrincipal = Clavier.lireCharLn();
}
//AFFICHAGE MENU CATEGORIES
if (selectionMenuPrincipal == '1') {
System.out.print(MSG_CATEGORIES);
selectionCategories = Clavier.lireCharLn();
//VALIDATION DE LA SELECTION CATEGORIE
while (selectionCategories < '1' || selectionCategories > '4') {
System.out.println(MSG_ERR_CATEGORIES);
selectionCategories = Clavier.lireCharLn();
}
//SELECTION DE LA CATEGORIE
if (selectionCategories == '1') {
System.out.print(MSG_MENU_ORDI);
selectionProduits = Clavier.lireCharLn();
System.out.print(MSG_NUM_MODELE);
selectionNumModele = Clavier.lireInt();
//VALIDATION DU NUMERO DE MODELE
while (selectionNumModele < 1000 || selectionNumModele > 9999999) {
System.out.println(MSG_ERR_NUM_MODELE);
selectionNumModele = Clavier.lireInt();
}
System.out.println("CODE DE PRODUIT : " + selectionCategories + selectionProduits + selectionNumModele);
System.out.print(MSG_RETOUR);
Clavier.lireFinLigne();
}else if (selectionCategories == '2') {
System.out.print(MSG_MENU_TELE);
selectionProduits = Clavier.lireChar();
System.out.print(MSG_NUM_MODELE);
selectionNumModele = Clavier.lireInt();
//VALIDATION DU NUMERO DE MODELE
while (selectionNumModele < 1000 || selectionNumModele >= 9999999) {
System.out.println(MSG_ERR_NUM_MODELE);
selectionNumModele = Clavier.lireInt();
}
System.out.println("CODE DE PRODUIT : " + selectionCategories + selectionProduits + selectionNumModele);
System.out.print(MSG_RETOUR);
Clavier.lireFinLigne();
}else if (selectionCategories == '3') {
System.out.print(MSG_NUM_MODELE);
selectionNumModele = Clavier.lireInt();
//VALIDATION DU NUMERO DE MODELE
while (selectionNumModele < 1000 || selectionNumModele >= 9999999) {
System.out.println(MSG_ERR_NUM_MODELE);
selectionNumModele = Clavier.lireInt();
}
System.out.println("CODE DE PRODUIT : " + selectionCategories + selectionNumModele);
System.out.print(MSG_RETOUR);
Clavier.lireFinLigne();
}else if (selectionCategories == '4') {
System.out.print(MSG_MENU_PELECTROS);
selectionProduits = Clavier.lireChar();
System.out.print(MSG_NUM_MODELE);
selectionNumModele = Clavier.lireInt();
//VALIDATION DU NUMERO DE MODELE
while (selectionNumModele < 1000 || selectionNumModele > 9999999) {
System.out.print(MSG_ERR_NUM_MODELE);
selectionNumModele = Clavier.lireInt();
}
System.out.println("CODE DE PRODUIT : " + selectionCategories + selectionProduits + selectionNumModele);
System.out.print(MSG_RETOUR);
Clavier.lireFinLigne();
}
//AFFICHAGE DU MENU D'INFORMATION DE CODE DE PRODUIT
}else if (selectionMenuPrincipal == '2') {
System.out.print(MSG_PRODUCT_CODE);
codeProduit = Clavier.lireInt();
while (codeProduit < 100000 || codeProduit > 999999999) {
System.out.print(MSG_ERR_CODE);
codeProduit = Clavier.lireInt();
}
}
}while (selectionMenuPrincipal !='3');
System.out.print(MSG_END_PROGRAM);
}
}
这是我正在努力的部分:
}else if (selectionMenuPrincipal == '2') {
System.out.print(MSG_PRODUCT_CODE);
codeProduit = Clavier.lireInt();
while (codeProduit < 100000 || codeProduit > 999999999) {
System.out.print(MSG_ERR_CODE);
codeProduit = Clavier.lireInt();
}
这是一个快速证明,以使用模量和分配10来构建型号,直到剩下2个数字为止。然后获取类别和产品。
public class NumberDecomposer {
public static void main(String[] args) {
decompose(1212345);
decompose(24123457);
}
private static void decompose(int modelCode) {
System.out.println("Original modelCode="+modelCode);
int factor = 1;
int modelNumber = 0;
while (modelCode >= 100) {
int lastDigit = modelCode % 10;
modelNumber += lastDigit * factor;
factor *= 10;
modelCode /= 10;
}
int productCode = modelCode % 10;
int category = modelCode / 10;
System.out.printf("Category=%d; Product=%d; Model number=%dn", category, productCode, modelNumber);
}
}
这是两个示例的输出:
Original modelCode=1212345
Category=1; Product=2; Model number=12345
Original modelCode=24123457
Category=2; Product=4; Model number=123457