从循环创建字符串会导致Java

时间:2019-02-09 21:29:17

标签: java string loops output letter

我需要能够将循环结果保存为字符串,以便能够操纵用户输出。没有数组

我试图在循环内将其转换为字符串,这没有多大意义。除非我采用其他方法,否则无法找到其他保存结果的方法。我不允许创建数组。

import UIKit
import FirebaseAuth
import Firebase
import FirebaseStorage

class ViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
    var ref : DatabaseReference?

    @IBOutlet weak var emailTextField: UITextField!
    @IBOutlet weak var passwordTextField: UITextField!
    @IBOutlet weak var myImage: UIImageView!
    @IBOutlet weak var userName: UITextField!
    @IBOutlet weak var ageTextField: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()
// get image from firebase
   // imagesFolder.

   // let imageBack = Database.database().reference().child("images").child("user")

   // self.myImage.image = UIImage(data: data!)

 // creating dataBase
    ref = Database.database().reference()

    if Auth.auth().currentUser != nil {
        ref?.child("images").child("username").setValue(["name":"first", "age":28])
    }

}
@IBAction func loginButtonPressed(_ sender: UIButton) {
}

// signUp Button pressed
@IBAction func signUpButtonPressed(_ sender: UIButton) {
    Auth.auth() .createUser(withEmail: emailTextField!.text!, password: passwordTextField!.text!) {(user,error)
    in
        if user != nil
        {
            self.performSegue(withIdentifier: "goToHome", sender: self)
            print("SignUp Sucessfull")
        }
        else {
            print("unSucessfull")
            // uiAlert
            let alert = UIAlertController(title:"wrong Information", message: nil, preferredStyle: .alert)
            let okButton = UIAlertAction(title: "ok", style: .default, handler: nil)
            alert.addAction(okButton)
            self.present(alert,animated: true,completion: nil)
            print("login Failed")
        }
        guard let userName = self.userName.text,!userName.isEmpty else {
            print("Email is Empty");return
        }
        guard let userAge = self.ageTextField.text,!userAge.isEmpty else {
            print("Age is required"); return
        }
}
    // upload image data to firebase
    let imagesFolder = Storage.storage().reference().child("images")
    if let image =  myImage.image {

        if let imageData = image.jpegData(compressionQuality: 0.75) {
        imagesFolder.child("\(NSUUID().uuidString).jpg").putData(imageData, metadata: nil,completion: { (metadata,error) in

            if let error = error {                  
                // alert notification
    }
            // putData(_:metadata:completion:)
        })                
    }
}

应该看起来像:即。 352-hey-call = “您的电话号码是352-439-2255”

1 个答案:

答案 0 :(得分:1)

实际上,对于每个字符,您都在寻找对应的字母或数字,但是您没有使用它,则需要将它们附加在一起。在String上使用+=运算符,但是为了获得更好的性能而处于循环中,建议使用StringBuilder

StringBuilder phone = new StringBuilder();
for (int i = 0; i < number.length(); i++){
    if (Character.isLetter(number.charAt(i))){
        phone.append(getNumber(Character.toUpperCase(number.charAt(i))));
    }else{
        phone.append(number.charAt(i));
    }
}

System.out.println("Your number is " + phone.toString());